001/**
002 * Copyright 2005-2014 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 */
016package org.kuali.rice.krad.test.document;
017
018import org.kuali.rice.kim.api.identity.Person;
019import org.kuali.rice.kns.maintenance.KualiMaintainableImpl;
020import org.kuali.rice.krad.exception.PessimisticLockingException;
021import org.kuali.rice.krad.util.GlobalVariables;
022
023/**
024 * This is a subclass of KualiMaintainableImpl designed specifically for testing custom lock descriptors. 
025 * 
026 * @author Kuali Rice Team (rice.collab@kuali.org)
027 */
028public class AccountType2MaintainableImpl extends KualiMaintainableImpl {
029        private static final long serialVersionUID = -396652994644246467L;
030
031    /**
032     * A test key that maps to a UserSession variable indicating what part of the maintainable or BO is editable. Note that this particular maintainable
033     * does not try to enforce such restrictions, since it is simply here for testing the custom lock descriptor functionality of PessimisticLockService.
034     */
035        public static final String ACCT_TYPE_2_MAINT_FIELDS_TO_EDIT = "acctType2MaintFieldsToEdit"; 
036    /**
037     * A test value indicating that the user has permission to edit only the AccountType2's accountTypeCode. This is here only for testing the
038     * generation of custom lock descriptors for PessimisticLockServiceTest; the maintainable and its BO do not actually enforce any such behavior.
039     */
040        public static final String EDIT_CODE_ONLY = "editCodeOnly";
041    /**
042     * A test value indicating that the user has permission to edit only the AccountType2's name. This is here only for testing the
043     * generation of custom lock descriptors for PessimisticLockServiceTest; the maintainable and its BO do not actually enforce any such behavior.
044     */
045        public static final String EDIT_NAME_ONLY = "editNameOnly";
046        
047        /**
048         * Since this class was made to test custom lock descriptors, this method will always return true.
049         * 
050         * @see org.kuali.rice.krad.maintenance.KualiMaintainableImpl#useCustomLockDescriptors()
051         */
052        @Override
053        public boolean useCustomLockDescriptors() {
054                return true;
055        }
056        
057        /**
058     * Generates a custom lock descriptor with a format of "[documentNumber]-[The user session's 'acctType2MaintFieldsToEdit' parameter value]".
059     * Will throw a PessimisticLockingException if this parameter is not defined or if its value is neither "editCodeOnly" nor "editNameOnly".
060         * 
061         * @see org.kuali.rice.krad.maintenance.KualiMaintainableImpl#getCustomLockDescriptor(org.kuali.rice.kim.api.identity.Person)
062         */
063        @Override
064        public String getCustomLockDescriptor(Person user) {
065        String fieldsToEdit = (String) GlobalVariables.getUserSession().retrieveObject(ACCT_TYPE_2_MAINT_FIELDS_TO_EDIT);
066        if (fieldsToEdit == null) {
067                throw new PessimisticLockingException("The user session's '" + ACCT_TYPE_2_MAINT_FIELDS_TO_EDIT + "' property cannot be null");
068        }
069        else if (!EDIT_CODE_ONLY.equals(fieldsToEdit) && !EDIT_NAME_ONLY.equals(fieldsToEdit)) {
070                throw new PessimisticLockingException("The value of the user session's '" + ACCT_TYPE_2_MAINT_FIELDS_TO_EDIT + "' property is invalid");
071        }
072        return getDocumentNumber() + "-" + fieldsToEdit;
073        }
074}