001    /**
002     * Copyright 2005-2012 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.document;
017    
018    import org.apache.cxf.common.util.StringUtils;
019    import org.apache.log4j.Logger;
020    import org.kuali.rice.kim.api.permission.Permission;
021    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
022    import org.kuali.rice.kim.impl.permission.GenericPermissionBo;
023    import org.kuali.rice.kim.impl.permission.PermissionBo;
024    import org.kuali.rice.kim.impl.permission.PermissionTemplateBo;
025    import org.kuali.rice.kim.impl.responsibility.ReviewResponsibilityBo;
026    import org.kuali.rice.kns.maintenance.KualiMaintainableImpl;
027    import org.kuali.rice.krad.bo.BusinessObject;
028    import org.kuali.rice.krad.bo.PersistableBusinessObject;
029    import org.kuali.rice.krad.util.KRADConstants;
030    
031    import java.util.HashMap;
032    
033    /**
034     * This is a description of what this class does - jonathan don't forget to fill this in. 
035     * 
036     * @author Kuali Rice Team (rice.collab@kuali.org)
037     *
038     */
039    public class GenericPermissionMaintainable extends KualiMaintainableImpl {
040    
041            private static final Logger LOG = Logger.getLogger( GenericPermissionMaintainable.class );      
042            private static final long serialVersionUID = -8102504656976243468L;
043    
044            /**
045             * Saves the responsibility via the responsibility update service
046             * 
047             * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#saveBusinessObject()
048             */
049            @Override
050            public void saveBusinessObject() {
051                    try {
052                            if ( LOG.isInfoEnabled() ) {
053                                    LOG.info( "Attempting to save Permission BO via PermissionService: " + getBusinessObject() );
054                            }
055                GenericPermissionBo genericPermissionBo = (GenericPermissionBo)getBusinessObject();
056                if (genericPermissionBo.getTemplateId() != null && genericPermissionBo.getTemplate() == null) {
057                    genericPermissionBo.setTemplate(
058                            PermissionTemplateBo.from(
059                                    KimApiServiceLocator.getPermissionService().getPermissionTemplate(genericPermissionBo.getTemplateId())));
060                }
061                            PermissionBo perm = GenericPermissionBo.toPermissionBo(genericPermissionBo);
062                            
063                            KimApiServiceLocator.getPermissionService().createPermission(PermissionBo.to(perm));
064                    } catch ( RuntimeException ex ) {
065                            LOG.error( "Exception in saveBusinessObject()", ex );
066                            throw ex;
067                    }
068            }
069    
070        /**
071         * @see org.kuali.rice.krad.maintenance.Maintainable#saveDataObject
072         */
073        @Override
074        public void saveDataObject() {
075            if (getDataObject() instanceof PersistableBusinessObject) {
076                GenericPermissionBo genericPermissionBo = (GenericPermissionBo)getDataObject();
077                boolean permissionExists = false;
078                if (genericPermissionBo.getId() != null) {
079                    permissionExists = KimApiServiceLocator.getPermissionService().getPermission(genericPermissionBo.getId()) != null;
080                }
081    
082                if (genericPermissionBo.getTemplateId() != null) {
083                    genericPermissionBo.setTemplate(
084                            PermissionTemplateBo.from(
085                                    KimApiServiceLocator.getPermissionService().getPermissionTemplate(genericPermissionBo.getTemplateId())));
086                }
087                PermissionBo perm = GenericPermissionBo.toPermissionBo(genericPermissionBo);
088                if (permissionExists) {
089                    KimApiServiceLocator.getPermissionService().updatePermission(PermissionBo.to(perm));
090                } else {
091                    //if its a copy the objectId should be empty and versionNumber should be null
092                    if(getMaintenanceAction().equals(KRADConstants.MAINTENANCE_COPY_ACTION)){
093                        if(org.apache.commons.lang.StringUtils.isNotBlank(perm.getObjectId())){
094                            perm.setObjectId("");
095                        }
096                        if(null!= perm.getVersionNumber()){
097                            perm.setVersionNumber(null);
098                        }
099                    }
100                    KimApiServiceLocator.getPermissionService().createPermission(PermissionBo.to(perm));
101                }
102                //getBusinessObjectService().linkAndSave((PersistableBusinessObject) dataObject);
103            } else {
104                throw new RuntimeException(
105                        "Cannot save object of type: " + getDataObjectClass() + " with permission service");
106            }
107        }
108            
109            /**
110             * This overridden method ...
111             * 
112             * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#getBoClass()
113             */
114            @Override
115            public Class<? extends PersistableBusinessObject> getBoClass() {
116                    return GenericPermissionBo.class;
117            }
118            
119            /**
120             * This overridden method ...
121             * 
122             * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#isExternalBusinessObject()
123             */
124            @Override
125            public boolean isExternalBusinessObject() {
126                    return true;
127            }
128            
129            /**
130             * This overridden method ...
131             * 
132             * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#prepareBusinessObject(org.kuali.rice.krad.bo.BusinessObject)
133             */
134            @Override
135            public void prepareBusinessObject(BusinessObject businessObject) {
136                    try {
137                            if ( businessObject == null ) {
138                                    throw new RuntimeException( "Configuration ERROR: GenericPermissionMaintainable.prepareBusinessObject passed a null object." );
139                            }
140                            if ( businessObject instanceof PermissionBo ) {
141                                    PermissionBo perm = getBusinessObjectService().findBySinglePrimaryKey(PermissionBo.class, ((PermissionBo)businessObject).getId() );
142                                    businessObject = new GenericPermissionBo(perm);
143                            } else if ( businessObject instanceof GenericPermissionBo ) {
144                                    // lookup the PermissionBo and convert to a GenericPermissionBo
145                                    PermissionBo perm = getBusinessObjectService().findBySinglePrimaryKey(PermissionBo.class, ((GenericPermissionBo)businessObject).getId() );
146                                    ((GenericPermissionBo)businessObject).loadFromPermission(perm);
147                            } else {
148                                    throw new RuntimeException( "Configuration ERROR: GenericPermissionMaintainable passed an unsupported object type: " + businessObject.getClass() );
149                            }
150                            if ( businessObject instanceof PersistableBusinessObject ) {
151                                    setBusinessObject( (PersistableBusinessObject)businessObject );
152                            }
153                            super.prepareBusinessObject(businessObject);
154                    } catch ( RuntimeException ex ) {
155                            LOG.error( "Exception in prepareBusinessObject()", ex );
156                            throw ex;
157                    }
158            }
159            
160    }