1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.module.purap.document.authorization;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.ole.module.purap.PurapParameterConstants;
20 import org.kuali.ole.module.purap.PurapPropertyConstants;
21 import org.kuali.ole.module.purap.businessobject.PurApAccountingLine;
22 import org.kuali.ole.module.purap.businessobject.PurApItem;
23 import org.kuali.ole.module.purap.document.*;
24 import org.kuali.ole.module.purap.document.service.PurapService;
25 import org.kuali.ole.sys.businessobject.AccountingLine;
26 import org.kuali.ole.sys.context.SpringContext;
27 import org.kuali.ole.sys.document.AccountingDocument;
28 import org.kuali.ole.sys.document.authorization.AccountingLineAuthorizerBase;
29 import org.kuali.ole.sys.document.authorization.FinancialSystemTransactionalDocumentAuthorizerBase;
30 import org.kuali.ole.sys.document.authorization.FinancialSystemTransactionalDocumentPresentationController;
31 import org.kuali.ole.sys.document.web.AccountingLineRenderingContext;
32 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
33 import org.kuali.rice.kim.api.identity.Person;
34 import org.kuali.rice.kns.datadictionary.TransactionalDocumentEntry;
35 import org.kuali.rice.kns.document.authorization.DocumentAuthorizer;
36 import org.kuali.rice.kns.service.DataDictionaryService;
37 import org.kuali.rice.krad.document.DocumentPresentationController;
38
39 import java.util.ArrayList;
40 import java.util.Collection;
41 import java.util.List;
42
43
44
45
46
47 public class PurapAccountingLineAuthorizer extends AccountingLineAuthorizerBase {
48
49
50
51
52
53
54
55
56
57
58 @Override
59 protected String getAddMethod(AccountingLine accountingLine, String accountingLineProperty) {
60 final String infix = getActionInfixForNewAccountingLine(accountingLine, accountingLineProperty);
61 String lineNumber = null;
62 if (accountingLineProperty.equals(PurapPropertyConstants.ACCOUNT_DISTRIBUTION_NEW_SRC_LINE)) {
63 lineNumber = "-2";
64 } else {
65 lineNumber = StringUtils.substringBetween(accountingLineProperty, "[", "]");
66 }
67 return "insert" + infix + "Line.line" + lineNumber + "." + "anchoraccounting" + infix + "Anchor";
68 }
69
70
71
72
73
74
75
76
77 @Override
78 protected String getDeleteLineMethod(AccountingLine accountingLine, String accountingLineProperty, Integer accountingLineIndex) {
79 final String infix = getActionInfixForExtantAccountingLine(accountingLine, accountingLineProperty);
80 String lineNumber = StringUtils.substringBetween(accountingLineProperty, "item[", "].sourceAccountingLine");
81 if (lineNumber == null) {
82 lineNumber = "-2";
83 }
84 String accountingLineNumber = StringUtils.substringBetween(accountingLineProperty, "sourceAccountingLine[", "]");
85 return "delete" + infix + "Line.line" + lineNumber + ":" + accountingLineNumber + ".anchoraccounting" + infix + "Anchor";
86 }
87
88
89
90
91
92
93
94
95 @Override
96 protected String getBalanceInquiryMethod(AccountingLine accountingLine, String accountingLineProperty, Integer accountingLineIndex) {
97 final String infix = getActionInfixForNewAccountingLine(accountingLine, accountingLineProperty);
98 String lineNumber = StringUtils.substringBetween(accountingLineProperty, "item[", "].sourceAccountingLine");
99 if (lineNumber == null) {
100 lineNumber = "-2";
101 }
102 String accountingLineNumber = StringUtils.substringBetween(accountingLineProperty, "sourceAccountingLine[", "]");
103 return "performBalanceInquiryFor" + infix + "Line.line" + ":" + lineNumber + ":" + accountingLineNumber + ".anchoraccounting" + infix + "existingLineLineAnchor" + accountingLineNumber;
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122 private boolean showAmountOnly(AccountingDocument accountingDocument) {
123 PurapService purapService = SpringContext.getBean(PurapService.class);
124 if (accountingDocument instanceof PurchasingAccountsPayableDocument)
125 if (purapService.isFullDocumentEntryCompleted((PurchasingAccountsPayableDocument) accountingDocument)) {
126 return true;
127 }
128 return false;
129 }
130
131
132
133
134
135 private FinancialSystemTransactionalDocumentPresentationController getPresentationController(AccountingDocument accountingDocument) {
136 final Class<? extends DocumentPresentationController> presentationControllerClass = ((TransactionalDocumentEntry) SpringContext.getBean(DataDictionaryService.class).getDataDictionary().getDictionaryObjectEntry(accountingDocument.getClass().getName())).getDocumentPresentationControllerClass();
137 FinancialSystemTransactionalDocumentPresentationController presentationController = null;
138 try {
139 presentationController = (FinancialSystemTransactionalDocumentPresentationController) presentationControllerClass.newInstance();
140 } catch (InstantiationException ie) {
141 throw new RuntimeException("Cannot instantiate instance of presentation controller for " + accountingDocument.getClass().getName(), ie);
142 } catch (IllegalAccessException iae) {
143 throw new RuntimeException("Cannot instantiate instance of presentation controller for " + accountingDocument.getClass().getName(), iae);
144 }
145 return presentationController;
146 }
147
148
149
150
151
152 protected FinancialSystemTransactionalDocumentAuthorizerBase getDocumentAuthorizer(AccountingDocument accountingDocument) {
153 final Class<? extends DocumentAuthorizer> documentAuthorizerClass = ((TransactionalDocumentEntry) SpringContext.getBean(DataDictionaryService.class).getDataDictionary().getDictionaryObjectEntry(accountingDocument.getClass().getName())).getDocumentAuthorizerClass();
154 FinancialSystemTransactionalDocumentAuthorizerBase documentAuthorizer = null;
155 try {
156 documentAuthorizer = (FinancialSystemTransactionalDocumentAuthorizerBase) documentAuthorizerClass.newInstance();
157 } catch (InstantiationException ie) {
158 throw new RuntimeException("Cannot instantiate instance of document authorizer for " + accountingDocument.getClass().getName(), ie);
159 } catch (IllegalAccessException iae) {
160 throw new RuntimeException("Cannot instantiate instance of document authorizer for " + accountingDocument.getClass().getName(), iae);
161 }
162 return documentAuthorizer;
163 }
164
165 @Override
166 public boolean isGroupEditable(AccountingDocument accountingDocument,
167 List<? extends AccountingLineRenderingContext> accountingLineRenderingContexts,
168 Person currentUser) {
169
170 boolean isEditable = super.isGroupEditable(accountingDocument, accountingLineRenderingContexts, currentUser);
171
172 if (isEditable) {
173 if (accountingLineRenderingContexts.size() == 0) {
174 return false;
175 }
176 isEditable = allowAccountingLinesAreEditable(accountingDocument, accountingLineRenderingContexts.get(0).getAccountingLine());
177 }
178
179 return isEditable;
180 }
181
182 @Override
183 public boolean determineEditPermissionOnField(AccountingDocument accountingDocument,
184 AccountingLine accountingLine,
185 String accountingLineCollectionProperty,
186 String fieldName,
187 boolean editablePage) {
188
189 boolean isEditable = super.determineEditPermissionOnField(accountingDocument, accountingLine, accountingLineCollectionProperty, fieldName, editablePage);
190
191 if (isEditable) {
192 isEditable = allowAccountingLinesAreEditable(accountingDocument, accountingLine);
193 }
194
195 return isEditable;
196 }
197
198 @Override
199 public boolean determineEditPermissionOnLine(AccountingDocument accountingDocument,
200 AccountingLine accountingLine,
201 String accountingLineCollectionProperty,
202 boolean currentUserIsDocumentInitiator,
203 boolean pageIsEditable) {
204
205 boolean isEditable = super.determineEditPermissionOnLine(accountingDocument, accountingLine, accountingLineCollectionProperty, currentUserIsDocumentInitiator, pageIsEditable);
206
207 if (isEditable) {
208 isEditable = allowAccountingLinesAreEditable(accountingDocument, accountingLine);
209 }
210
211 return (isEditable && pageIsEditable);
212 }
213
214
215
216
217 protected boolean allowAccountingLinesAreEditable(AccountingDocument accountingDocument,
218 AccountingLine accountingLine) {
219
220 PurApAccountingLine purapAccount = (PurApAccountingLine) accountingLine;
221 @SuppressWarnings("rawtypes")
222 Class clazz = getPurapDocumentClass(accountingDocument);
223 if (clazz == null) {
224 return true;
225 }
226
227
228 PurchasingAccountsPayableDocumentBase purapDoc = (PurchasingAccountsPayableDocumentBase) accountingDocument;
229 if (!purapDoc.isCalculated()) {
230 return true;
231 }
232
233 Collection<String> restrictedItemTypesList = new ArrayList<String>(SpringContext.getBean(ParameterService.class).getParameterValuesAsString(clazz, PurapParameterConstants.PURAP_ITEM_TYPES_RESTRICTING_ACCOUNT_EDIT));
234
235
236
237 boolean retval = true;
238 retval = isEditableBasedOnTradeInRestriction(accountingDocument, accountingLine);
239
240 if (restrictedItemTypesList != null && purapAccount.getPurapItem() != null) {
241 return (!restrictedItemTypesList.contains(((PurApItem) purapAccount.getPurapItem()).getItemTypeCode()) && retval);
242 } else if (restrictedItemTypesList != null && purapAccount.getPurapItem() == null) {
243 return retval;
244 } else {
245 return true;
246 }
247 }
248
249
250
251
252
253
254
255
256
257
258
259
260
261 protected PurApItem findTheItemForAccountingLine(AccountingDocument accountingDocument, AccountingLine accountingLine) {
262 PurApItem retval = null;
263 List<PurApItem> listItems = null;
264
265 scan:
266 {
267 if (accountingDocument instanceof PurchasingAccountsPayableDocumentBase) {
268 listItems = ((PurchasingAccountsPayableDocumentBase) accountingDocument).getItems();
269
270
271
272 for (PurApItem oneItem : listItems) {
273 List<PurApAccountingLine> acctLines = oneItem.getSourceAccountingLines();
274 for (PurApAccountingLine oneAcctLine : acctLines) {
275
276 if (oneAcctLine == accountingLine) {
277 retval = oneItem;
278
279
280 break scan;
281 }
282 }
283 }
284 }
285 }
286
287 return retval;
288 }
289
290
291
292
293
294
295
296
297
298
299
300
301
302 private boolean isEditableBasedOnTradeInRestriction(AccountingDocument accountingDocument, AccountingLine accountingLine) {
303 boolean retval = true;
304
305
306 if ((accountingLine != null) && (accountingLine instanceof PurApAccountingLine)) {
307 PurApItem purApItem = (((PurApAccountingLine) accountingLine)).getPurapItem();
308
309
310
311
312
313 if (purApItem == null) {
314 purApItem = findTheItemForAccountingLine(accountingDocument, accountingLine);
315 }
316
317 if (purApItem != null) {
318 String itemTypeCode = purApItem.getItemTypeCode();
319 if (itemTypeCode.toUpperCase().equalsIgnoreCase(PurapParameterConstants.PURAP_ITEM_TYPE_TRDI)) {
320
321 if ((accountingLine.getChartOfAccountsCode() != null) || (accountingLine.getAccountNumber() != null) || (accountingLine.getFinancialObjectCode() != null)) {
322 retval = false;
323 }
324 }
325
326
327 }
328 }
329 return retval;
330 }
331
332 @SuppressWarnings("rawtypes")
333 private Class getPurapDocumentClass(AccountingDocument accountingDocument) {
334 if (accountingDocument instanceof RequisitionDocument) {
335 return RequisitionDocument.class;
336 } else if (accountingDocument instanceof PurchaseOrderDocument) {
337 return PurchaseOrderDocument.class;
338 } else if (accountingDocument instanceof PaymentRequestDocument) {
339 return PaymentRequestDocument.class;
340 } else {
341 return null;
342 }
343 }
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362 @Override
363 protected boolean approvedForUnqualifiedEditing(AccountingDocument accountingDocument, AccountingLine accountingLine, String accountingLineCollectionProperty, boolean currentUserIsDocumentInitiator) {
364 boolean retval = true;
365
366 retval = isEditableBasedOnTradeInRestriction(accountingDocument, accountingLine);
367
368 if (retval) {
369 retval = super.approvedForUnqualifiedEditing(accountingDocument, accountingLine, accountingLineCollectionProperty, currentUserIsDocumentInitiator);
370 }
371 return retval;
372 }
373 }
374