View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kim.impl.responsibility;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.kuali.rice.kew.api.KewApiConstants;
21  import org.kuali.rice.kim.api.KimConstants;
22  import org.kuali.rice.kim.api.common.template.Template;
23  import org.kuali.rice.kim.api.responsibility.Responsibility;
24  import org.kuali.rice.kim.api.responsibility.ResponsibilityContract;
25  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
26  import org.kuali.rice.kim.impl.common.attribute.KimAttributeDataBo;
27  import org.kuali.rice.kim.impl.permission.PermissionTemplateBo;
28  import org.kuali.rice.kim.impl.services.KimImplServiceLocator;
29  import org.kuali.rice.kns.document.MaintenanceDocument;
30  import org.kuali.rice.kns.maintenance.KualiMaintainableImpl;
31  import org.kuali.rice.kns.maintenance.Maintainable;
32  import org.kuali.rice.kns.service.KNSServiceLocator;
33  import org.kuali.rice.kns.web.ui.Field;
34  import org.kuali.rice.kns.web.ui.Row;
35  import org.kuali.rice.kns.web.ui.Section;
36  import org.kuali.rice.krad.bo.BusinessObject;
37  import org.kuali.rice.krad.bo.PersistableBusinessObject;
38  import org.kuali.rice.krad.data.KradDataServiceLocator;
39  import org.kuali.rice.krad.data.platform.MaxValueIncrementerFactory;
40  import org.kuali.rice.krad.util.KRADConstants;
41  import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
42  
43  import java.util.HashMap;
44  import java.util.List;
45  import java.util.Map;
46  
47  public class ReviewResponsibilityMaintainable extends KualiMaintainableImpl {
48  
49  	private static final Logger LOG = Logger.getLogger( ReviewResponsibilityMaintainable.class );
50  	private static final long serialVersionUID = -8102504656976243468L;
51  
52      protected static final String DOCUMENT_TYPE_NAME = "documentTypeName";
53      protected static final String ROUTE_NODE_NAME = "routeNodeName";
54  
55  	private static Template REVIEW_TEMPLATE;
56  
57      public List getSections(MaintenanceDocument document, Maintainable oldMaintainable) {
58          List<Section> sections = super.getSections(document, oldMaintainable);
59          if(document.isEdit()){
60          	for (Section section : sections) {
61                  for (Row row : section.getRows()) {
62                      for (Field field : row.getFields()) {
63                      	if(ReviewResponsibilityBo.ACTION_DETAILS_AT_ROLE_MEMBER_LEVEL_FIELD_NAME.equals(field.getPropertyName())){
64                      		field.setReadOnly(true);
65                      	}
66                      }
67                  }
68          	}
69          }
70          return sections;
71      }
72                  
73  	/**
74  	 * Saves the responsibility via the responsibility service
75  	 * 
76  	 * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#saveDataObject()
77  	 */
78  	@Override
79  	public void saveDataObject() {
80          if ( LOG.isInfoEnabled() ) {
81              LOG.info( "Attempting to save ReviewResponsibilityBo BO via ResponsibilityService:" + getBusinessObject() );
82          }
83          populateReviewTemplateInfo();
84  
85          ReviewResponsibilityBo resp = (ReviewResponsibilityBo)getDataObject();
86          // build the Map<String, String> with the details
87          Map<String, String> details = new HashMap<String, String>();
88          details.put( KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME, resp.getDocumentTypeName() );
89          details.put( KimConstants.AttributeConstants.ROUTE_NODE_NAME, resp.getRouteNodeName() );
90          details.put( KimConstants.AttributeConstants.REQUIRED, resp.isRequired()?"true":"false" );
91          details.put( KimConstants.AttributeConstants.ACTION_DETAILS_AT_ROLE_MEMBER_LEVEL, resp.isActionDetailsAtRoleMemberLevel()?"true":"false" );
92          if ( StringUtils.isNotBlank(resp.getQualifierResolverProvidedIdentifier()) ) {
93              details.put( KimConstants.AttributeConstants.QUALIFIER_RESOLVER_PROVIDED_IDENTIFIER, resp.getQualifierResolverProvidedIdentifier() );
94          }
95  
96          //ResponsibilityBo bo = ReviewResponsibilityBo.toResponsibilityBo(resp);
97          if (resp.getTemplateId() == null) {
98              resp.setTemplateId(REVIEW_TEMPLATE.getId());
99              resp.setTemplate(ResponsibilityTemplateBo.from(REVIEW_TEMPLATE));
100         }
101 
102         else {
103                 resp.setTemplate(
104                         ResponsibilityTemplateBo.from(KimApiServiceLocator.getResponsibilityService().getResponsibilityTemplate(
105                                 resp.getTemplateId())));
106         }
107         //set attributes
108         resp.setAttributes(details);
109         resp.setAttributeDetails(KimAttributeDataBo.createFrom(ResponsibilityAttributeBo.class, details, resp.getTemplate().getKimTypeId()));
110 
111         boolean responsibilityExists = false;
112         if (resp.getId() != null) {
113             responsibilityExists = KimApiServiceLocator.getResponsibilityService().getResponsibility(resp.getId()) != null;
114         }
115 
116         if (responsibilityExists) {
117             KimApiServiceLocator.getResponsibilityService().updateResponsibility(ResponsibilityBo.to(resp));
118         } else {
119             //if its a copy action the objectId should be  empty and versionNumber should be null.
120             if(getMaintenanceAction().equals(KRADConstants.MAINTENANCE_COPY_ACTION)){
121                 if(org.apache.commons.lang.StringUtils.isNotBlank(resp.getObjectId())){
122                     resp.setObjectId("");
123                 }
124                 if(null!= resp.getVersionNumber()){
125                     resp.setVersionNumber(null);
126                 }
127             }
128             KimApiServiceLocator.getResponsibilityService().createResponsibility(ResponsibilityBo.to(resp));
129         }
130 	}
131 	
132 	private static synchronized void populateReviewTemplateInfo() {
133 		if ( REVIEW_TEMPLATE == null ) {
134 		    REVIEW_TEMPLATE = KimApiServiceLocator.getResponsibilityService().findRespTemplateByNamespaceCodeAndName(KewApiConstants.KEW_NAMESPACE, KewApiConstants.DEFAULT_RESPONSIBILITY_TEMPLATE_NAME);
135         }
136 	}
137 
138 	@Override
139 	public Class<? extends BusinessObject> getBoClass() {
140 		return ReviewResponsibilityBo.class;
141 	}
142 	
143 	@Override
144 	public Class<?> getDataObjectClass() {
145         return ReviewResponsibilityBo.class;
146 	}
147 
148 	@Override
149 	public void prepareBusinessObject(BusinessObject businessObject) {
150         if ( businessObject == null ) {
151             throw new RuntimeException( "Configuration ERROR: ReviewResponsibilityBoMaintainable.prepareBusinessObject passed a null object." );
152         }
153         if ( businessObject instanceof ResponsibilityContract ) {
154             ResponsibilityBo resp = KradDataServiceLocator.getDataObjectService().find(ResponsibilityBo.class, ((ResponsibilityContract)businessObject).getId() );
155             businessObject = new ReviewResponsibilityBo( resp );
156             setDataObject(businessObject);
157         } else {
158             throw new RuntimeException( "Configuration ERROR: ReviewResponsibilityBoMaintainable passed an unsupported object type: " + businessObject.getClass() );
159         }
160         super.prepareBusinessObject(businessObject);
161 	}
162 
163 	@Override
164 	public boolean isExternalBusinessObject() {
165 		return true;
166 	}
167 
168     @Override
169     public void processAfterNew(MaintenanceDocument document, Map<String, String[]> requestParameters) {
170         super.processAfterNew(document, requestParameters);
171 
172         initializeResponsibilityId(document.getDocumentBusinessObject());
173     }
174 
175     @Override
176     public void processAfterCopy(MaintenanceDocument document, Map<String, String[]> parameters) {
177         super.processAfterCopy(document, parameters);
178 
179         initializeResponsibilityId(document.getNewMaintainableObject().getDataObject());
180     }
181 
182     private void initializeResponsibilityId(Object dataObject) {
183         if (dataObject instanceof ReviewResponsibilityBo) {
184             ReviewResponsibilityBo responsibilityBo = (ReviewResponsibilityBo) dataObject;
185 
186             if (StringUtils.isBlank(responsibilityBo.getId())) {
187                 DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(
188                         KimImplServiceLocator.getDataSource(), KimConstants.SequenceNames.KRIM_RSP_ID_S);
189                 responsibilityBo.setId(incrementer.nextStringValue());
190             }
191         }
192     }
193 
194     @Override
195     public void setupNewFromExisting(MaintenanceDocument document, Map<String, String[]> parameters) {
196         String docTypeName = "";
197         String routeNodeName = "";
198 
199         ReviewResponsibilityBo reviewResponsibilityBo = (ReviewResponsibilityBo) document.getNewMaintainableObject().getDataObject();
200         initializeResponsibilityId(document.getDocumentBusinessObject());
201         reviewResponsibilityBo.setActive(true);
202 
203         for (String paramName : parameters.keySet()) {
204             String[] parameterValues = parameters.get(paramName);
205 
206             if (paramName.equals(DOCUMENT_TYPE_NAME)) {
207                 if (parameterValues.length > 0) {
208                     docTypeName = parameterValues[0];
209                 }
210             }
211 
212             if (paramName.equals(ROUTE_NODE_NAME)) {
213                 if (parameterValues.length > 0) {
214                     routeNodeName = parameterValues[0];
215                 }
216             }
217         }
218 
219         if (StringUtils.isNotEmpty(docTypeName) && StringUtils.isNotEmpty(routeNodeName)) {
220             reviewResponsibilityBo.setDocumentTypeName(docTypeName);
221             reviewResponsibilityBo.setRouteNodeName(routeNodeName);
222         }
223 
224         document.getNewMaintainableObject().setDataObject(reviewResponsibilityBo);
225     }
226 
227 }