001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.bo;
017
018import org.kuali.rice.krad.datadictionary.DataDictionaryLocationConfigurer;
019import org.kuali.rice.krad.service.DataDictionaryService;
020import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
021import org.kuali.rice.krad.service.PersistenceService;
022import org.springframework.beans.factory.InitializingBean;
023import org.springframework.context.ApplicationContext;
024import org.springframework.context.ApplicationContextAware;
025
026import javax.persistence.EntityManager;
027import java.lang.reflect.Modifier;
028import java.util.ArrayList;
029import java.util.Collections;
030import java.util.List;
031import java.util.Map;
032
033/**
034 * This is a description of what this class does - bhargavp don't forget to fill this in.
035 *
036 * @author Kuali Rice Team (rice.collab@kuali.org)
037 *
038 */
039public class ModuleConfiguration implements InitializingBean, ApplicationContextAware {
040
041        //protected static Logger LOG = Logger.getLogger(ModuleConfiguration.class);
042
043        protected String namespaceCode;
044        protected ApplicationContext applicationContext;
045
046        protected List<String> packagePrefixes;
047
048        protected List<String> databaseRepositoryFilePaths;
049
050        protected List<String> dataDictionaryPackages;
051
052        protected List<String> scriptConfigurationFilePaths;
053
054        protected List<String> jobNames;
055
056        protected List<String> triggerNames;
057
058        //optional
059        protected String dataSourceName;
060
061        //optional
062        protected EntityManager entityManager;
063
064        protected Map<Class, Class> externalizableBusinessObjectImplementations;
065
066        protected boolean initializeDataDictionary;
067
068        protected PersistenceService persistenceService;
069
070        protected DataDictionaryService dataDictionaryService;
071
072        public ModuleConfiguration() {
073                databaseRepositoryFilePaths = new ArrayList<String>();
074                dataDictionaryPackages = new ArrayList<String>();
075                scriptConfigurationFilePaths = new ArrayList<String>();
076                jobNames = new ArrayList<String>();
077                triggerNames = new ArrayList<String>();
078        }
079
080        /**
081         * @return the databaseRepositoryFilePaths
082         */
083        public List<String> getDatabaseRepositoryFilePaths() {
084                return this.databaseRepositoryFilePaths;
085        }
086
087        /**
088         * @param databaseRepositoryFilePaths the databaseRepositoryFilePaths to set
089         */
090        public void setDatabaseRepositoryFilePaths(
091                        List<String> databaseRepositoryFilePaths) {
092                this.trimList(databaseRepositoryFilePaths);     
093                this.databaseRepositoryFilePaths = databaseRepositoryFilePaths;
094        }
095
096        /**
097         * @return the dataDictionaryPackages
098         */
099        public List<String> getDataDictionaryPackages() {
100                return this.dataDictionaryPackages;
101        }
102
103        /**
104         * @param dataDictionaryPackages the dataDictionaryPackages to set
105         */
106        public void setDataDictionaryPackages(List<String> dataDictionaryPackages) {
107                this.trimList(dataDictionaryPackages);                  
108                this.dataDictionaryPackages = dataDictionaryPackages;           
109        }       
110        
111        /**
112         * @return the externalizableBusinessObjectImplementations
113         */
114        public Map<Class, Class> getExternalizableBusinessObjectImplementations() {
115                if (this.externalizableBusinessObjectImplementations == null)
116                        return null;
117                return Collections.unmodifiableMap(this.externalizableBusinessObjectImplementations);
118        }
119
120        /**
121         * @param externalizableBusinessObjectImplementations the externalizableBusinessObjectImplementations to set
122         */
123        public void setExternalizableBusinessObjectImplementations(
124                        Map<Class, Class> externalizableBusinessObjectImplementations) {
125                if (externalizableBusinessObjectImplementations != null) {
126                        for (Class implClass : externalizableBusinessObjectImplementations.values()) {
127                                int implModifiers = implClass.getModifiers();
128                                if (Modifier.isInterface(implModifiers) || Modifier.isAbstract(implModifiers)) {
129                                        throw new RuntimeException("Externalizable business object implementation class " +
130                                                        implClass.getName() + " must be a non-interface, non-abstract class");
131                                }
132                        }
133                }
134                this.externalizableBusinessObjectImplementations = externalizableBusinessObjectImplementations;
135        }
136
137        public List<String> getPackagePrefixes(){
138                return this.packagePrefixes;
139        }
140
141        public void setPackagePrefixes(List<String> packagePrefixes){
142                this.trimList(packagePrefixes);
143                this.packagePrefixes = packagePrefixes;
144        }
145
146        public void setInitializeDataDictionary(boolean initializeDataDictionary){
147                this.initializeDataDictionary = initializeDataDictionary;
148        }
149
150        public List<String> getScriptConfigurationFilePaths(){
151                return this.scriptConfigurationFilePaths;
152        }
153
154        /**
155         * @return the jobNames
156         */
157        public List<String> getJobNames() {
158                return this.jobNames;
159        }
160
161        /**
162         * @param jobNames the jobNames to set
163         */
164        public void setJobNames(List<String> jobNames) {
165                this.jobNames = jobNames;
166        }
167
168
169        /**
170         * @return the triggerNames
171         */
172        public List<String> getTriggerNames() {
173                return this.triggerNames;
174        }
175
176        /**
177         * @param triggerNames the triggerNames to set
178         */
179        public void setTriggerNames(List<String> triggerNames) {
180                this.triggerNames = triggerNames;
181        }
182
183        /**
184         * @return the initializeDataDictionary
185         */
186        public boolean isInitializeDataDictionary() {
187                return this.initializeDataDictionary;
188        }
189
190        /**
191         * @param scriptConfigurationFilePaths the scriptConfigurationFilePaths to set
192         */
193        public void setScriptConfigurationFilePaths(
194                        List<String> scriptConfigurationFilePaths) {
195                this.scriptConfigurationFilePaths = scriptConfigurationFilePaths;
196        }
197
198        @Override
199        public void afterPropertiesSet() throws Exception {
200                if (isInitializeDataDictionary() && getDataDictionaryPackages() != null && !getDataDictionaryPackages().isEmpty() ) {
201                        if ( getDataDictionaryService() == null ) {
202                                setDataDictionaryService(KRADServiceLocatorWeb.getDataDictionaryService());
203                        }
204                        if ( getDataDictionaryService() == null ) {
205                                setDataDictionaryService((DataDictionaryService)applicationContext.getBean( KRADServiceLocatorWeb.DATA_DICTIONARY_SERVICE ));
206                        }
207                        DataDictionaryLocationConfigurer ddl = new DataDictionaryLocationConfigurer( getDataDictionaryService() );
208                        ddl.setDataDictionaryPackages(getDataDictionaryPackages());
209                        ddl.afterPropertiesSet();
210                }
211                if (getDatabaseRepositoryFilePaths() != null) {
212                    for (String repositoryLocation : getDatabaseRepositoryFilePaths()) {
213                                // Need the OJB persistence service because it is the only one ever using the database repository files
214                        if (getPersistenceService() == null) {
215                                setPersistenceService(KRADServiceLocatorWeb.getPersistenceServiceOjb());
216                        }
217                        if ( persistenceService == null ) {
218                                setPersistenceService((PersistenceService)applicationContext.getBean( KRADServiceLocatorWeb.PERSISTENCE_SERVICE_OJB  ));
219                        }
220                        getPersistenceService().loadRepositoryDescriptor( repositoryLocation );
221                        }
222                }
223        }
224
225        /**
226         * @return the namespaceCode
227         */
228        public String getNamespaceCode() {
229                return this.namespaceCode;
230        }
231
232        /**
233         * @param namespaceCode the namespaceCode to set
234         */
235        public void setNamespaceCode(String namespaceCode) {
236                this.namespaceCode = namespaceCode;
237        }
238
239        @Override
240        public void setApplicationContext(ApplicationContext applicationContext) {
241                this.applicationContext = applicationContext;
242        }
243
244        /**
245         * @return the dataDictionaryService
246         */
247        public DataDictionaryService getDataDictionaryService() {
248                return this.dataDictionaryService;
249        }
250
251        /**
252         * @param dataDictionaryService the dataDictionaryService to set
253         */
254        public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
255                this.dataDictionaryService = dataDictionaryService;
256        }
257
258        /**
259         * @return the persistenceService
260         */
261        public PersistenceService getPersistenceService() {
262                return this.persistenceService;
263        }
264
265        /**
266         * @param persistenceService the persistenceService to set
267         */
268        public void setPersistenceService(PersistenceService persistenceService) {
269                this.persistenceService = persistenceService;
270        }
271
272    public String getDataSourceName() {
273        return this.dataSourceName;
274    }
275
276    public void setDataSourceName(String dataSourceName) {
277        this.dataSourceName = dataSourceName;
278    }
279
280    public EntityManager getEntityManager() {
281        return this.entityManager;
282    }
283
284    public void setEntityManager(EntityManager entityManager) {
285        this.entityManager = entityManager;
286    }
287    
288    /**
289         * 
290         * This method passes by reference. It will alter the list passed in.
291         * 
292         * @param stringList
293         */
294        protected void trimList(List<String> stringList){
295                if(stringList != null){
296                        // we need to trim whitespace from the stringList. Because trim() creates a new string 
297                        // we have to explicitly put the new string back into the list
298                        for(int i=0; i<stringList.size(); i++){
299                                String elmt = stringList.get(i);                                
300                                elmt = elmt.trim();
301                                stringList.set(i, elmt);
302                        }                       
303                }
304        }
305
306}