001    /**
002     * Copyright 2005-2013 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     */
016    package org.kuali.rice.kew.service.impl;
017    
018    import org.joda.time.DateTime;
019    import org.kuali.rice.core.api.config.module.RunMode;
020    import org.kuali.rice.kew.api.KewApiConstants;
021    import org.kuali.rice.kew.api.KewApiServiceLocator;
022    import org.kuali.rice.kew.api.doctype.DocumentTypeService;
023    import org.kuali.rice.kew.api.document.Document;
024    import org.kuali.rice.kew.api.document.DocumentStatus;
025    import org.kuali.rice.kew.docsearch.DocumentSearchCriteriaEbo;
026    import org.kuali.rice.kew.doctype.bo.DocumentType;
027    import org.kuali.rice.kew.doctype.bo.DocumentTypeEBO;
028    import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
029    import org.kuali.rice.krad.service.impl.ModuleServiceBase;
030    
031    import java.util.ArrayList;
032    import java.util.List;
033    import java.util.Map;
034    
035    /**
036     * The ModuleService for KEW
037     *
038     * @author Kuali Rice Team (rice.collab@kuali.org)
039     *
040     */
041    public class KEWModuleService extends ModuleServiceBase {
042    
043            protected DocumentTypeService docTypeService = null;
044    
045            /**
046             * These are the "primary" keys for the DocTypeService. We are considering both
047             * name and documentTypeId to be unique.
048             *
049             * @see org.kuali.rice.krad.service.impl.ModuleServiceBase#listPrimaryKeyFieldNames(java.lang.Class)
050             */
051            @Override
052            public List<String> listPrimaryKeyFieldNames(Class businessObjectInterfaceClass) {
053                    if ( DocumentTypeEBO.class.isAssignableFrom( businessObjectInterfaceClass ) ) {
054                            List<String> pkFields = new ArrayList<String>( 1 );
055                            pkFields.add( "documentTypeId" );
056                            return pkFields;
057                    }else if(DocumentSearchCriteriaEbo.class.isAssignableFrom( businessObjectInterfaceClass )){
058                            List<String> pkFields = new ArrayList<String>( 1 );
059                            pkFields.add( "documentId" );
060                            return pkFields;
061                    }
062                    return super.listPrimaryKeyFieldNames(businessObjectInterfaceClass);
063            }
064    
065            /**
066             * This overridden method calls the DocumentTypeService instead of the underlying
067             * KNS service.  Allows you to search on name and docTypeId
068             *
069             * @see org.kuali.rice.krad.service.impl.ModuleServiceBase#getExternalizableBusinessObject(java.lang.Class, java.util.Map)
070             */
071            @Override
072            public <T extends ExternalizableBusinessObject> T getExternalizableBusinessObject(
073                            Class<T> businessObjectClass, Map<String, Object> fieldValues) {
074    
075                    if(DocumentTypeEBO.class.isAssignableFrom(businessObjectClass)){
076    
077                org.kuali.rice.kew.api.doctype.DocumentType fetchedDocumentType = null;
078    
079                if ( fieldValues.containsKey( "name" ) ) {
080                    fetchedDocumentType = getDocumentTypeService().getDocumentTypeByName((String) fieldValues.get("name"));
081                            }else if( fieldValues.containsKey( "documentTypeId" ) ){
082                    fetchedDocumentType = getDocumentTypeService().getDocumentTypeById(fieldValues.get("documentTypeId").toString());
083                            }else if (fieldValues.containsKey( "id" ) ) {
084                                    // assume it's a string and convert it to a long.
085                    fetchedDocumentType = getDocumentTypeService().getDocumentTypeById(fieldValues.get("id").toString());
086                            }
087    
088                if (fetchedDocumentType != null) {
089                    // convert to EBO
090                    return (T) DocumentType.from(fetchedDocumentType);
091                } else {
092                    return null;
093                }
094    
095                    }else if(DocumentSearchCriteriaEbo.class.isAssignableFrom( businessObjectClass )){
096                            if ( fieldValues.containsKey( "documentId" ) ) {
097                                    return (T)createDocumentSearchEbo(KewApiServiceLocator.getWorkflowDocumentService().getDocument(
098                            fieldValues.get("documentId").toString()));
099                            }
100    
101                    }
102    
103                    // otherwise, use the default implementation
104                    return super.getExternalizableBusinessObject(businessObjectClass, fieldValues);
105            }
106    
107            /**
108             * @return the docTypeService
109             */
110            protected synchronized DocumentTypeService getDocumentTypeService() {
111                    if(this.docTypeService == null){
112                            // the default
113                            this.docTypeService = KewApiServiceLocator.getDocumentTypeService();
114                    }
115                    return this.docTypeService;
116            }
117    
118            /**
119             * @param docTypeService the docTypeService to set
120             */
121            public synchronized void setDocumentTypeService(DocumentTypeService docTypeService) {
122                    this.docTypeService = docTypeService;
123            }
124    
125            private DocumentSearchCriteriaEbo createDocumentSearchEbo(final Document doc){
126                    return new DocumentSearchCriteriaEbo(){
127    
128                @Override
129                public String getApplicationDocumentId() {
130                    return doc.getApplicationDocumentId();
131                }
132    
133                @Override
134                public DocumentStatus getStatus() {
135                    return doc.getStatus();
136                }
137    
138                @Override
139                public String getApplicationDocumentStatus() {
140                    return doc.getApplicationDocumentStatus();
141                }
142    
143                @Override
144                public String getTitle() {
145                    return doc.getTitle();
146                }
147    
148                @Override
149                public String getDocumentTypeName() {
150                    return doc.getDocumentTypeName();
151                }
152    
153                @Override
154                public String getInitiatorPrincipalId() {
155                    return doc.getInitiatorPrincipalId();
156                }
157    
158                @Override
159                public String getDocumentId() {
160                    return doc.getDocumentId();
161                }
162    
163                @Override
164                public DateTime getDateCreated() {
165                    return doc.getDateCreated();
166                }
167    
168                @Override
169                public void refresh() {
170                    // do nothing
171                }
172                
173            };
174            }
175            /**
176             * This overridden method rewrites the URL.
177             *
178             * @see org.kuali.rice.krad.service.impl.ModuleServiceBase#getExternalizableBusinessObjectInquiryUrl(java.lang.Class, java.util.Map)
179             */
180            @Override
181            public String getExternalizableBusinessObjectInquiryUrl(
182                            Class inquiryBusinessObjectClass, Map<String, String[]> parameters) {
183                    if ( DocumentTypeEBO.class.isAssignableFrom( inquiryBusinessObjectClass ) ) {
184                            int nonBlank = 0;
185                            boolean nameFound = false;
186                            //"name" is the only non-blank property passed in
187                            for(String key: parameters.keySet()){
188                                    if("name".equals(key) && parameters.get(key) != null){
189                                            nameFound=true;
190                                    }else if(!"name".equals(key) && parameters.get(key) != null){
191                                            nonBlank ++;
192                                    }
193                            }
194    
195                            if(nonBlank == 0 && nameFound == true){
196                                    parameters.clear(); // clear out other parameters, including the name pass in
197                                    DocumentTypeEBO dte = (DocumentTypeEBO) DocumentType.from(getDocumentTypeService().getDocumentTypeByName(parameters.get( "name" )[0] ));
198                                    String[] strArr = {dte.getDocumentTypeId().toString()};
199                                    parameters.put("documentTypeId", strArr);
200                            }
201    
202                    }
203    
204                    return super.getExternalizableBusinessObjectInquiryUrl(
205                                    inquiryBusinessObjectClass, parameters);
206            }
207    
208        /**
209             * We want to be able to use name as an alternate key
210             *
211             * @see org.kuali.rice.krad.service.ModuleService#listAlternatePrimaryKeyFieldNames(java.lang.Class)
212             */
213            public List<List<String>> listAlternatePrimaryKeyFieldNames(
214                            Class businessObjectInterfaceClass) {
215                    if ( DocumentTypeEBO.class.isAssignableFrom( businessObjectInterfaceClass ) ) {
216                            ArrayList<List<String>> retList = new ArrayList<List<String>>();
217                            ArrayList<String> keyList = new ArrayList<String>();
218    
219                            keyList.add("name");
220                            retList.add(keyList);
221                            return retList;
222                    }else{
223                            return null;
224                    }
225    
226            }
227    
228            @Override
229        public boolean goToCentralRiceForInquiry() {
230            RunMode runMode = getRunMode(KewApiConstants.Namespaces.MODULE_NAME);
231    
232            if (RunMode.EMBEDDED.equals(runMode)) {
233                return true;
234            } else {
235                return false;
236            }
237        }
238    }
239