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.krad.document.SessionDocument;
020import org.kuali.rice.krad.document.TransactionalDocumentBase;
021import org.kuali.rice.krad.exception.PessimisticLockingException;
022import org.kuali.rice.krad.test.document.bo.Account;
023import org.kuali.rice.krad.util.GlobalVariables;
024import org.kuali.rice.krad.util.NoteType;
025
026import java.util.ArrayList;
027import java.util.List;
028
029/**
030 * This is a test copy of AccountRequestDocument that has been modified to allow for custom lock descriptors.
031 * 
032 * @author Kuali Rice Team (rice.collab@kuali.org)
033 */
034public class AccountRequestDocument2 extends TransactionalDocumentBase implements SessionDocument {
035
036    private static final long serialVersionUID = 7616690365412064056L;
037    
038    /**
039     * A test key to map to some value indicating what fields the user can edit on this doc. Note that such behavior is not enforced; it is simply
040     * here for testing the PessimisticLockService's custom lock descriptor functionality.
041     */
042    public static final String ACCT_REQ_DOC_2_EDITABLE_FIELDS = "acctReqDoc2EditableFields";
043    /**
044     * A test value indicating that the user has permission to edit everything except "reason1" and "reason2". This is here only for testing the
045     * generation of custom lock descriptors for PessimisticLockServiceTest; AccountRequestDocument2 does not actually enforce any such behavior.
046     */
047    public static final String EDIT_ALL_BUT_REASONS = "editAllButReasons";
048    /**
049     * A test value indicating that the user only has permission to edit "reason1" and "reason2". This is here only for testing the generation of
050     * custom lock descriptors for PessimisticLockServiceTest; AccountRequestDocument2 does not actually enforce any such behavior.
051     */
052    public static final String EDIT_REASONS_ONLY = "editReasonsOnly";
053    
054        private String requester;
055    private String reason1;
056    private String reason2;
057    private String requestType;
058    private String accountTypeCode;
059
060    private List<Account> accounts;
061
062    public AccountRequestDocument2() {
063        accounts = new ArrayList<Account>();
064    }
065
066    public String getReason2() {
067        return reason2;
068    }
069
070    public void setReason2(String reason2) {
071        this.reason2 = reason2;
072    }
073
074    public String getReason1() {
075        return reason1;
076    }
077
078    public void setReason1(String reason1) {
079        this.reason1 = reason1;
080    }
081
082    public String getRequester() {
083        return requester;
084    }
085
086    public void setRequester(String requester) {
087        this.requester = requester;
088    }
089
090    public List<Account> getAccounts() {
091        return accounts;
092    }
093
094    public void setAccounts(List<Account> accounts) {
095        this.accounts = accounts;
096    }
097
098    public Account getAccount(int index) {
099        while(accounts.size() - 1 < index) {
100            accounts.add(new Account());
101        }
102        return accounts.get(index);
103    }
104
105    public String getRequestType() {
106        return requestType;
107    }
108
109    public void setRequestType(String requestType) {
110        this.requestType = requestType;
111    }
112
113    public void setAccountTypeCode(String accountType) {
114        this.accountTypeCode = accountType;
115    }
116
117    public String getAccountTypeCode() {
118        return accountTypeCode;
119    }
120
121    /**
122     * Since this class was made to test custom lock descriptors, this method will always return true.
123     * 
124     * @see org.kuali.rice.krad.document.DocumentBase#useCustomLockDescriptors()
125     */
126    @Override
127    public boolean useCustomLockDescriptors() {
128        return true;
129    }
130    
131    /**
132     * Generates a custom lock descriptor with a format of "[documentNumber]-[The user session's 'acctReqDoc2EditableFields' parameter value]".
133     * Will throw a PessimisticLockingException if this parameter is not defined or if its value is neither "editAllButReasons" nor "editReasonsOnly".
134     * 
135     * @see org.kuali.rice.krad.document.DocumentBase#getCustomLockDescriptor(org.kuali.rice.kim.api.identity.Person)
136     */
137    @Override
138    public String getCustomLockDescriptor(Person user) {
139        String editableFields = (String) GlobalVariables.getUserSession().retrieveObject(ACCT_REQ_DOC_2_EDITABLE_FIELDS);
140        if (editableFields == null) {
141                throw new PessimisticLockingException("The user session's '" + ACCT_REQ_DOC_2_EDITABLE_FIELDS + "' property cannot be null");
142        }
143        else if (!EDIT_ALL_BUT_REASONS.equals(editableFields) && !EDIT_REASONS_ONLY.equals(editableFields)) {
144                throw new PessimisticLockingException("The value of the user session's '" + ACCT_REQ_DOC_2_EDITABLE_FIELDS + "' property is invalid");
145        }
146        return getDocumentNumber() + "-" + editableFields;
147    }
148
149    @Override
150    public NoteType getNoteType() {
151        return NoteType.BUSINESS_OBJECT;
152    }
153}