View Javadoc

1   /**
2    * Copyright 2005-2011 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.services.KimApiServiceLocator;
25  import org.kuali.rice.kim.impl.common.attribute.KimAttributeDataBo;
26  import org.kuali.rice.kim.impl.permission.PermissionTemplateBo;
27  import org.kuali.rice.kns.document.MaintenanceDocument;
28  import org.kuali.rice.kns.maintenance.KualiMaintainableImpl;
29  import org.kuali.rice.kns.maintenance.Maintainable;
30  import org.kuali.rice.kns.web.ui.Field;
31  import org.kuali.rice.kns.web.ui.Row;
32  import org.kuali.rice.kns.web.ui.Section;
33  import org.kuali.rice.krad.bo.BusinessObject;
34  import org.kuali.rice.krad.bo.PersistableBusinessObject;
35  
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Map;
39  
40  public class ReviewResponsibilityMaintainable extends KualiMaintainableImpl {
41  
42  	private static final Logger LOG = Logger.getLogger( ReviewResponsibilityMaintainable.class );
43  	private static final long serialVersionUID = -8102504656976243468L;
44  
45  	private static Template REVIEW_TEMPLATE;
46  
47      public List getSections(MaintenanceDocument document, Maintainable oldMaintainable) {
48          List<Section> sections = super.getSections(document, oldMaintainable);
49          if(document.isEdit()){
50          	for (Section section : sections) {
51                  for (Row row : section.getRows()) {
52                      for (Field field : row.getFields()) {
53                      	if(ReviewResponsibilityBo.ACTION_DETAILS_AT_ROLE_MEMBER_LEVEL_FIELD_NAME.equals(field.getPropertyName())){
54                      		field.setReadOnly(true);
55                      	}
56                      }
57                  }
58          	}
59          }
60          return sections;
61      }
62                  
63  	/**
64  	 * Saves the responsibility via the responsibility service
65  	 * 
66  	 * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#saveDataObject()
67  	 */
68  	@Override
69  	public void saveDataObject() {
70          if ( LOG.isInfoEnabled() ) {
71              LOG.info( "Attempting to save ReviewResponsibilityBo BO via ResponsibilityService:" + getBusinessObject() );
72          }
73          populateReviewTemplateInfo();
74  
75          ReviewResponsibilityBo resp = (ReviewResponsibilityBo)getDataObject();
76          // build the Map<String, String> with the details
77          Map<String, String> details = new HashMap<String, String>();
78          details.put( KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME, resp.getDocumentTypeName() );
79          details.put( KimConstants.AttributeConstants.ROUTE_NODE_NAME, resp.getRouteNodeName() );
80          details.put( KimConstants.AttributeConstants.REQUIRED, resp.isRequired()?"true":"false" );
81          details.put( KimConstants.AttributeConstants.ACTION_DETAILS_AT_ROLE_MEMBER_LEVEL, resp.isActionDetailsAtRoleMemberLevel()?"true":"false" );
82          if ( StringUtils.isNotBlank(resp.getQualifierResolverProvidedIdentifier()) ) {
83              details.put( KimConstants.AttributeConstants.QUALIFIER_RESOLVER_PROVIDED_IDENTIFIER, resp.getQualifierResolverProvidedIdentifier() );
84          }
85  
86          //ResponsibilityBo bo = ReviewResponsibilityBo.toResponsibilityBo(resp);
87          if (resp.getTemplateId() == null) {
88              resp.setTemplateId(REVIEW_TEMPLATE.getId());
89              resp.setTemplate(ResponsibilityTemplateBo.from(REVIEW_TEMPLATE));
90          }
91          if (resp.getTemplateId() != null && resp.getTemplate() == null) {
92                  resp.setTemplate(
93                          ResponsibilityTemplateBo.from(KimApiServiceLocator.getResponsibilityService().getResponsibilityTemplate(
94                                  resp.getTemplateId())));
95          }
96          //set attributes
97          resp.setAttributes(details);
98          //resp.setAttributeDetails(KimAttributeDataBo.createFrom(ResponsibilityAttributeBo.class, details, resp.getTemplate().getKimTypeId()));
99  
100         boolean responsibilityExists = false;
101         if (resp.getId() != null) {
102             responsibilityExists = KimApiServiceLocator.getResponsibilityService().getResponsibility(resp.getId()) != null;
103         }
104 
105         if (responsibilityExists) {
106             KimApiServiceLocator.getResponsibilityService().updateResponsibility(ResponsibilityBo.to(resp));
107         } else {
108             KimApiServiceLocator.getResponsibilityService().createResponsibility(ResponsibilityBo.to(resp));
109         }
110 	}
111 	
112 	private static synchronized void populateReviewTemplateInfo() {
113 		if ( REVIEW_TEMPLATE == null ) {
114 		    REVIEW_TEMPLATE = KimApiServiceLocator.getResponsibilityService().findRespTemplateByNamespaceCodeAndName(KewApiConstants.KEW_NAMESPACE, KewApiConstants.DEFAULT_RESPONSIBILITY_TEMPLATE_NAME);
115         }
116 	}
117 
118 	@Override
119 	public Class<? extends BusinessObject> getBoClass() {
120 		return ReviewResponsibilityBo.class;
121 	}
122 
123 	@Override
124 	public void prepareBusinessObject(BusinessObject businessObject) {
125         if ( businessObject == null ) {
126             throw new RuntimeException( "Configuration ERROR: ReviewResponsibilityBoMaintainable.prepareBusinessObject passed a null object." );
127         }
128         if ( businessObject instanceof ResponsibilityBo ) {
129             ResponsibilityBo resp = getBusinessObjectService().findBySinglePrimaryKey(ResponsibilityBo.class, ((ResponsibilityBo)businessObject).getId() );
130             businessObject = new ReviewResponsibilityBo( resp );
131             setBusinessObject( (PersistableBusinessObject)businessObject );
132         } else {
133             throw new RuntimeException( "Configuration ERROR: ReviewResponsibilityBoMaintainable passed an unsupported object type: " + businessObject.getClass() );
134         }
135         super.prepareBusinessObject(businessObject);
136 	}
137 
138     /**
139 	 * This overridden method ...
140 	 *
141 	 * @see org.kuali.rice.krad.maintenance.KualiMaintainableImpl#isExternalBusinessObject()
142 	 */
143 	@Override
144 	public boolean isExternalBusinessObject() {
145 		return true;
146 	}
147 
148 }