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