View Javadoc
1   package org.kuali.ole.deliver.util;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.kuali.ole.OLEConstants;
5   import org.kuali.ole.deliver.bo.OleLoanDocument;
6   import org.kuali.rice.kim.api.permission.PermissionService;
7   import org.kuali.rice.kim.api.services.KimApiServiceLocator;
8   import org.kuali.rice.krad.util.GlobalVariables;
9   
10  import java.util.HashMap;
11  import java.util.Set;
12  import java.util.StringTokenizer;
13  
14  /**
15   * Created by pvsubrah on 3/24/15.
16   */
17  public class HandleErrorsAndOverridePermissions {
18  
19      private static HandleErrorsAndOverridePermissions handleErrorsAndOverridePermissions;
20  
21      private HandleErrorsAndOverridePermissions() {
22  
23      }
24  
25      public static HandleErrorsAndOverridePermissions getInstance() {
26          if (null== handleErrorsAndOverridePermissions) {
27              handleErrorsAndOverridePermissions = new HandleErrorsAndOverridePermissions();
28          }
29          return handleErrorsAndOverridePermissions;
30      }
31  
32  
33      public void handle(OleLoanDocument oleLoanDocument, String operatorId, org.kuali.ole.docstore.common.document.content.instance.Item item) {
34          HashMap<String, String> errorsAndPermission;
35          StringBuffer failures = new StringBuffer();
36  
37          errorsAndPermission = oleLoanDocument.getErrorsAndPermission();
38          PermissionService service = KimApiServiceLocator.getPermissionService();
39  
40          int i = 1;
41          if (errorsAndPermission != null) {
42              Set<String> errorMessage = errorsAndPermission.keySet();
43              if (errorMessage != null && errorMessage.size() > 0) {
44                  for (String errMsg : errorMessage) {
45                      if (StringUtils.isNotEmpty(errMsg)) {
46                          oleLoanDocument.getErrorsAndPermission().putAll(errorsAndPermission);
47                          if (oleLoanDocument.isRenewalItemFlag()) {
48                              String permission = errorsAndPermission.get(errMsg);
49                              if (operatorId == null) {
50                                  if (GlobalVariables.getUserSession() != null) {
51                                      operatorId = GlobalVariables.getUserSession().getPrincipalId();
52                                  }
53                              }
54                              boolean hasRenewPermission = service.hasPermission(operatorId, OLEConstants.DLVR_NMSPC, permission);
55                              if (!hasRenewPermission) {
56                                  oleLoanDocument.setRenewPermission(false);
57                              } else {
58                                  oleLoanDocument.setRenewPermission(true);
59                              }
60                              failures.append(errMsg + OLEConstants.OR);
61                          } else {
62                              failures.append(i++ + ". " + errMsg + OLEConstants.BREAK);
63                          }
64                      }
65                  }
66              }
67          }
68  
69          if (oleLoanDocument.getErrorMessage() != null && StringUtils.isNotBlank(oleLoanDocument.getErrorMessage())) {
70              StringTokenizer stringTokenizer = new StringTokenizer(oleLoanDocument.getErrorMessage(), "\n");
71  
72              while (stringTokenizer.hasMoreTokens()) {
73                  String errMsg = stringTokenizer.nextToken();
74                  if (errMsg.equalsIgnoreCase("Inform the current borrower that this item has some missing pieces before proceeding with checkout.")) {
75                      if (item != null && item.getMissingPiecesCount() != null) {
76                          errMsg = errMsg.replace("some", item.getMissingPiecesCount());
77                      }
78                  }
79                  if (oleLoanDocument.isRenewalItemFlag()) {
80                      failures.append(errMsg + OLEConstants.OR);
81                  } else {
82                      if (errMsg.equalsIgnoreCase("Item status is Lost")) {
83                          //oleLoanDocument.setStatusLost(true);
84                          failures.append(i++ + "." + errMsg + "-" + OLEConstants.ITEMSTATUSLOST + OLEConstants.BREAK);
85                      } else {
86                          failures.append(i++ + ". " + errMsg + OLEConstants.BREAK);
87                      }
88                  }
89              }
90          }
91      }
92  }