View Javadoc

1   /*
2    * Copyright 2007-2009 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.kns.test.document;
17  
18  import java.util.ArrayList;
19  import java.util.LinkedHashMap;
20  import java.util.List;
21  
22  import org.kuali.rice.kim.bo.Person;
23  import org.kuali.rice.kns.document.SessionDocument;
24  import org.kuali.rice.kns.document.TransactionalDocumentBase;
25  import org.kuali.rice.kns.exception.PessimisticLockingException;
26  import org.kuali.rice.kns.test.document.bo.Account;
27  import org.kuali.rice.kns.util.GlobalVariables;
28  
29  /**
30   * This is a test copy of AccountRequestDocument that has been modified to allow for custom lock descriptors.
31   * 
32   * @author Kuali Rice Team (rice.collab@kuali.org)
33   */
34  public class AccountRequestDocument2 extends TransactionalDocumentBase implements SessionDocument {
35  
36      private static final long serialVersionUID = 7616690365412064056L;
37      
38      /**
39       * 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
40       * here for testing the PessimisticLockService's custom lock descriptor functionality.
41       */
42      public static final String ACCT_REQ_DOC_2_EDITABLE_FIELDS = "acctReqDoc2EditableFields";
43      /**
44       * A test value indicating that the user has permission to edit everything except "reason1" and "reason2". This is here only for testing the
45       * generation of custom lock descriptors for PessimisticLockServiceTest; AccountRequestDocument2 does not actually enforce any such behavior.
46       */
47      public static final String EDIT_ALL_BUT_REASONS = "editAllButReasons";
48      /**
49       * A test value indicating that the user only has permission to edit "reason1" and "reason2". This is here only for testing the generation of
50       * custom lock descriptors for PessimisticLockServiceTest; AccountRequestDocument2 does not actually enforce any such behavior.
51       */
52      public static final String EDIT_REASONS_ONLY = "editReasonsOnly";
53      
54  	private String requester;
55      private String reason1;
56      private String reason2;
57      private String requestType;
58      private String accountTypeCode;
59  
60      private List<Account> accounts;
61  
62      public AccountRequestDocument2() {
63          accounts = new ArrayList<Account>();
64      }
65  
66      @Override
67      protected LinkedHashMap<?,?> toStringMapper() {
68          LinkedHashMap<String, String> meMap = new LinkedHashMap<String, String>();
69          meMap.put("requester", getRequester());
70          meMap.put("reason1", getReason1());
71          meMap.put("reason2", getReason2());
72          return meMap;
73      }
74  
75      public String getReason2() {
76          return reason2;
77      }
78  
79      public void setReason2(String reason2) {
80          this.reason2 = reason2;
81      }
82  
83      public String getReason1() {
84          return reason1;
85      }
86  
87      public void setReason1(String reason1) {
88          this.reason1 = reason1;
89      }
90  
91      public String getRequester() {
92          return requester;
93      }
94  
95      public void setRequester(String requester) {
96          this.requester = requester;
97      }
98  
99      public List<Account> getAccounts() {
100         return accounts;
101     }
102 
103     public void setAccounts(List<Account> accounts) {
104         this.accounts = accounts;
105     }
106 
107     public Account getAccount(int index) {
108         while(accounts.size() - 1 < index) {
109             accounts.add(new Account());
110         }
111         return accounts.get(index);
112     }
113 
114     public String getRequestType() {
115         return requestType;
116     }
117 
118     public void setRequestType(String requestType) {
119         this.requestType = requestType;
120     }
121 
122     public void setAccountTypeCode(String accountType) {
123         this.accountTypeCode = accountType;
124     }
125 
126     public String getAccountTypeCode() {
127         return accountTypeCode;
128     }
129 
130     /**
131      * Since this class was made to test custom lock descriptors, this method will always return true.
132      * 
133      * @see org.kuali.rice.kns.document.DocumentBase#useCustomLockDescriptors()
134      */
135     @Override
136     public boolean useCustomLockDescriptors() {
137     	return true;
138     }
139     
140     /**
141      * Generates a custom lock descriptor with a format of "[documentNumber]-[The user session's 'acctReqDoc2EditableFields' parameter value]".
142      * Will throw a PessimisticLockingException if this parameter is not defined or if its value is neither "editAllButReasons" nor "editReasonsOnly".
143      * 
144      * @see org.kuali.rice.kns.document.DocumentBase#getCustomLockDescriptor(org.kuali.rice.kim.bo.Person)
145      */
146     @Override
147     public String getCustomLockDescriptor(Person user) {
148     	String editableFields = (String) GlobalVariables.getUserSession().retrieveObject(ACCT_REQ_DOC_2_EDITABLE_FIELDS);
149     	if (editableFields == null) {
150     		throw new PessimisticLockingException("The user session's '" + ACCT_REQ_DOC_2_EDITABLE_FIELDS + "' property cannot be null");
151     	}
152     	else if (!EDIT_ALL_BUT_REASONS.equals(editableFields) && !EDIT_REASONS_ONLY.equals(editableFields)) {
153     		throw new PessimisticLockingException("The value of the user session's '" + ACCT_REQ_DOC_2_EDITABLE_FIELDS + "' property is invalid");
154     	}
155     	return getDocumentNumber() + "-" + editableFields;
156     }
157 }