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.kim.impl.responsibility;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.apache.log4j.Logger;
020    import org.kuali.rice.kew.api.KewApiConstants;
021    import org.kuali.rice.kim.api.KimConstants;
022    import org.kuali.rice.kim.api.common.template.Template;
023    import org.kuali.rice.kim.api.responsibility.Responsibility;
024    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
025    import org.kuali.rice.kim.impl.common.attribute.KimAttributeDataBo;
026    import org.kuali.rice.kim.impl.permission.PermissionTemplateBo;
027    import org.kuali.rice.kns.document.MaintenanceDocument;
028    import org.kuali.rice.kns.maintenance.KualiMaintainableImpl;
029    import org.kuali.rice.kns.maintenance.Maintainable;
030    import org.kuali.rice.kns.service.KNSServiceLocator;
031    import org.kuali.rice.kns.web.ui.Field;
032    import org.kuali.rice.kns.web.ui.Row;
033    import org.kuali.rice.kns.web.ui.Section;
034    import org.kuali.rice.krad.bo.BusinessObject;
035    import org.kuali.rice.krad.bo.PersistableBusinessObject;
036    import org.kuali.rice.krad.util.KRADConstants;
037    
038    import java.util.HashMap;
039    import java.util.List;
040    import java.util.Map;
041    
042    public class ReviewResponsibilityMaintainable extends KualiMaintainableImpl {
043    
044            private static final Logger LOG = Logger.getLogger( ReviewResponsibilityMaintainable.class );
045            private static final long serialVersionUID = -8102504656976243468L;
046    
047            private static Template REVIEW_TEMPLATE;
048    
049        public List getSections(MaintenanceDocument document, Maintainable oldMaintainable) {
050            List<Section> sections = super.getSections(document, oldMaintainable);
051            if(document.isEdit()){
052                    for (Section section : sections) {
053                    for (Row row : section.getRows()) {
054                        for (Field field : row.getFields()) {
055                            if(ReviewResponsibilityBo.ACTION_DETAILS_AT_ROLE_MEMBER_LEVEL_FIELD_NAME.equals(field.getPropertyName())){
056                                    field.setReadOnly(true);
057                            }
058                        }
059                    }
060                    }
061            }
062            return sections;
063        }
064                    
065            /**
066             * Saves the responsibility via the responsibility service
067             * 
068             * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#saveDataObject()
069             */
070            @Override
071            public void saveDataObject() {
072            if ( LOG.isInfoEnabled() ) {
073                LOG.info( "Attempting to save ReviewResponsibilityBo BO via ResponsibilityService:" + getBusinessObject() );
074            }
075            populateReviewTemplateInfo();
076    
077            ReviewResponsibilityBo resp = (ReviewResponsibilityBo)getDataObject();
078            // build the Map<String, String> with the details
079            Map<String, String> details = new HashMap<String, String>();
080            details.put( KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME, resp.getDocumentTypeName() );
081            details.put( KimConstants.AttributeConstants.ROUTE_NODE_NAME, resp.getRouteNodeName() );
082            details.put( KimConstants.AttributeConstants.REQUIRED, resp.isRequired()?"true":"false" );
083            details.put( KimConstants.AttributeConstants.ACTION_DETAILS_AT_ROLE_MEMBER_LEVEL, resp.isActionDetailsAtRoleMemberLevel()?"true":"false" );
084            if ( StringUtils.isNotBlank(resp.getQualifierResolverProvidedIdentifier()) ) {
085                details.put( KimConstants.AttributeConstants.QUALIFIER_RESOLVER_PROVIDED_IDENTIFIER, resp.getQualifierResolverProvidedIdentifier() );
086            }
087    
088            //ResponsibilityBo bo = ReviewResponsibilityBo.toResponsibilityBo(resp);
089            if (resp.getTemplateId() == null) {
090                resp.setTemplateId(REVIEW_TEMPLATE.getId());
091                resp.setTemplate(ResponsibilityTemplateBo.from(REVIEW_TEMPLATE));
092            }
093    
094            else {
095                    resp.setTemplate(
096                            ResponsibilityTemplateBo.from(KimApiServiceLocator.getResponsibilityService().getResponsibilityTemplate(
097                                    resp.getTemplateId())));
098            }
099            //set attributes
100            resp.setAttributes(details);
101            resp.setAttributeDetails(KimAttributeDataBo.createFrom(ResponsibilityAttributeBo.class, details, resp.getTemplate().getKimTypeId()));
102    
103            boolean responsibilityExists = false;
104            if (resp.getId() != null) {
105                responsibilityExists = KimApiServiceLocator.getResponsibilityService().getResponsibility(resp.getId()) != null;
106            }
107    
108            if (responsibilityExists) {
109                KimApiServiceLocator.getResponsibilityService().updateResponsibility(ResponsibilityBo.to(resp));
110            } else {
111                //if its a copy action the objectId should be  empty and versionNumber should be null.
112                if(getMaintenanceAction().equals(KRADConstants.MAINTENANCE_COPY_ACTION)){
113                    if(org.apache.commons.lang.StringUtils.isNotBlank(resp.getObjectId())){
114                        resp.setObjectId("");
115                    }
116                    if(null!= resp.getVersionNumber()){
117                        resp.setVersionNumber(null);
118                    }
119                }
120                KimApiServiceLocator.getResponsibilityService().createResponsibility(ResponsibilityBo.to(resp));
121            }
122            }
123            
124            private static synchronized void populateReviewTemplateInfo() {
125                    if ( REVIEW_TEMPLATE == null ) {
126                        REVIEW_TEMPLATE = KimApiServiceLocator.getResponsibilityService().findRespTemplateByNamespaceCodeAndName(KewApiConstants.KEW_NAMESPACE, KewApiConstants.DEFAULT_RESPONSIBILITY_TEMPLATE_NAME);
127            }
128            }
129    
130            @Override
131            public Class<? extends BusinessObject> getBoClass() {
132                    return ReviewResponsibilityBo.class;
133            }
134    
135            @Override
136            public void prepareBusinessObject(BusinessObject businessObject) {
137            if ( businessObject == null ) {
138                throw new RuntimeException( "Configuration ERROR: ReviewResponsibilityBoMaintainable.prepareBusinessObject passed a null object." );
139            }
140            if ( businessObject instanceof ResponsibilityBo ) {
141                ResponsibilityBo resp = KNSServiceLocator.getBusinessObjectService().findBySinglePrimaryKey(ResponsibilityBo.class, ((ResponsibilityBo)businessObject).getId() );
142                businessObject = new ReviewResponsibilityBo( resp );
143                setBusinessObject( (PersistableBusinessObject)businessObject );
144            } else {
145                throw new RuntimeException( "Configuration ERROR: ReviewResponsibilityBoMaintainable passed an unsupported object type: " + businessObject.getClass() );
146            }
147            super.prepareBusinessObject(businessObject);
148            }
149    
150        /**
151             * This overridden method ...
152             *
153             * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#isExternalBusinessObject()
154             */
155            @Override
156            public boolean isExternalBusinessObject() {
157                    return true;
158            }
159    
160    }