View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.test.document;
17  
18  import org.kuali.rice.kim.api.identity.Person;
19  import org.kuali.rice.krad.document.SessionDocument;
20  import org.kuali.rice.krad.document.TransactionalDocumentBase;
21  import org.kuali.rice.krad.exception.PessimisticLockingException;
22  import org.kuali.rice.krad.test.document.bo.Account;
23  import org.kuali.rice.krad.util.GlobalVariables;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  /**
29   * This is a test copy of AccountRequestDocument that has been modified to allow for custom lock descriptors.
30   * 
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   */
33  public class AccountRequestDocument2 extends TransactionalDocumentBase implements SessionDocument {
34  
35      private static final long serialVersionUID = 7616690365412064056L;
36      
37      /**
38       * 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
39       * here for testing the PessimisticLockService's custom lock descriptor functionality.
40       */
41      public static final String ACCT_REQ_DOC_2_EDITABLE_FIELDS = "acctReqDoc2EditableFields";
42      /**
43       * A test value indicating that the user has permission to edit everything except "reason1" and "reason2". This is here only for testing the
44       * generation of custom lock descriptors for PessimisticLockServiceTest; AccountRequestDocument2 does not actually enforce any such behavior.
45       */
46      public static final String EDIT_ALL_BUT_REASONS = "editAllButReasons";
47      /**
48       * A test value indicating that the user only has permission to edit "reason1" and "reason2". This is here only for testing the generation of
49       * custom lock descriptors for PessimisticLockServiceTest; AccountRequestDocument2 does not actually enforce any such behavior.
50       */
51      public static final String EDIT_REASONS_ONLY = "editReasonsOnly";
52      
53  	private String requester;
54      private String reason1;
55      private String reason2;
56      private String requestType;
57      private String accountTypeCode;
58  
59      private List<Account> accounts;
60  
61      public AccountRequestDocument2() {
62          accounts = new ArrayList<Account>();
63      }
64  
65      public String getReason2() {
66          return reason2;
67      }
68  
69      public void setReason2(String reason2) {
70          this.reason2 = reason2;
71      }
72  
73      public String getReason1() {
74          return reason1;
75      }
76  
77      public void setReason1(String reason1) {
78          this.reason1 = reason1;
79      }
80  
81      public String getRequester() {
82          return requester;
83      }
84  
85      public void setRequester(String requester) {
86          this.requester = requester;
87      }
88  
89      public List<Account> getAccounts() {
90          return accounts;
91      }
92  
93      public void setAccounts(List<Account> accounts) {
94          this.accounts = accounts;
95      }
96  
97      public Account getAccount(int index) {
98          while(accounts.size() - 1 < index) {
99              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 }