View Javadoc
1   package org.kuali.student.enrollment.class1.hold.service.impl;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.kuali.rice.core.api.criteria.PredicateFactory;
5   import org.kuali.rice.core.api.criteria.QueryByCriteria;
6   import org.kuali.rice.core.api.util.RiceKeyConstants;
7   import org.kuali.rice.krad.util.GlobalVariables;
8   import org.kuali.rice.krad.util.KRADConstants;
9   import org.kuali.student.common.collection.KSCollectionUtils;
10  import org.kuali.student.common.uif.service.impl.KSViewHelperServiceImpl;
11  import org.kuali.student.common.util.security.ContextUtils;
12  import org.kuali.student.common.util.security.SecurityUtils;
13  import org.kuali.student.core.person.dto.PersonAffiliationInfo;
14  import org.kuali.student.core.person.dto.PersonInfo;
15  import org.kuali.student.core.person.service.impl.PersonServiceConstants;
16  import org.kuali.student.enrollment.class1.hold.form.AppliedHoldManagementForm;
17  import org.kuali.student.enrollment.class1.hold.form.AppliedHoldResult;
18  import org.kuali.student.enrollment.class1.hold.form.HoldIssueManagementForm;
19  import org.kuali.student.enrollment.class1.hold.form.HoldIssueResult;
20  import org.kuali.student.enrollment.class1.hold.service.HoldsViewHelperService;
21  import org.kuali.student.enrollment.class1.hold.util.HoldsConstants;
22  import org.kuali.student.enrollment.class1.hold.util.HoldsResourceLoader;
23  import org.kuali.student.enrollment.class1.hold.util.HoldsUtil;
24  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
25  import org.kuali.student.r2.core.acal.dto.TermInfo;
26  import org.kuali.student.r2.core.constants.HoldServiceConstants;
27  import org.kuali.student.r2.core.hold.dto.AppliedHoldInfo;
28  import org.kuali.student.r2.core.hold.dto.HoldIssueInfo;
29  
30  import java.util.ArrayList;
31  import java.util.Collections;
32  import java.util.HashMap;
33  import java.util.List;
34  import java.util.Map;
35  
36  /**
37   * Created with IntelliJ IDEA.
38   * User: Blue Team (SA)
39   * Date: 17 July 2014
40   * <p/>
41   * Implementation of the HoldIssueViewHelperService that contains helper methods that support the Hold Issue Management Controller.
42   */
43  public class HoldsViewHelperServiceImpl extends KSViewHelperServiceImpl implements HoldsViewHelperService {
44  
45      /**
46       * This method is used to search for hold issues and map them to HoldIssueResult
47       *
48       * @param holdIssueFrom
49       * @return List holdIssueResultList
50       */
51      @Override
52      public List<HoldIssueResult> searchHolds(HoldIssueManagementForm holdIssueFrom) {
53  
54          try {
55              Map<String, String> searchCriteria = new HashMap<String, String>();
56              searchCriteria.put(HoldsConstants.HOLD_ISSUE_NAME, holdIssueFrom.getName());
57              searchCriteria.put(HoldsConstants.HOLD_ISSUE_HOLD_CODE, holdIssueFrom.getCode());
58              searchCriteria.put(HoldsConstants.HOLD_ISSUE_TYPE_KEY, holdIssueFrom.getTypeKey());
59              searchCriteria.put(HoldsConstants.HOLD_ISSUE_STATE_KEY, holdIssueFrom.getState());
60              searchCriteria.put(HoldsConstants.HOLD_ISSUE_ORG_ID, holdIssueFrom.getOrganizationId());
61              searchCriteria.put(HoldsConstants.HOLD_ISSUE_DESCR_PLAIN, holdIssueFrom.getDescr());
62  
63              QueryByCriteria.Builder query = HoldsUtil.buildQueryByCriteria(searchCriteria);
64  
65              return HoldsUtil.createHoldIssueResultList(HoldsResourceLoader.getHoldService().searchForHoldIssues(query.build(), createContextInfo()));
66  
67          } catch (Exception e) {
68              convertServiceExceptionsToUI(e);
69          }
70          return null;
71      }
72  
73  
74      @Override
75      public PersonInfo getStudentById(String studentId) {
76  
77          if (StringUtils.isBlank(studentId)) {
78              GlobalVariables.getMessageMap().putError(HoldsConstants.APPLIED_HOLDS_PERSON_ID, HoldsConstants.APPLIED_HOLDS_MSG_ERROR_STUDENT_REQUIRED);
79              return null;
80          }
81  
82          try {
83              PersonInfo personInfo = HoldsResourceLoader.getPersonService().getPerson(studentId.toUpperCase(), createContextInfo());
84  
85              Boolean validStudent = false;
86              List<PersonAffiliationInfo> personAffiliationInfos = HoldsResourceLoader.getPersonService().getPersonAffiliationsByPerson(personInfo.getId(), createContextInfo());
87              for (PersonAffiliationInfo personAffiliationInfo : personAffiliationInfos) {
88                  if (personAffiliationInfo.getTypeKey().equals(PersonServiceConstants.PERSON_AFFILIATION_STUDENT_TYPE_KEY)) {
89                      validStudent = true;
90                  }
91              }
92              if (!validStudent) {
93                  GlobalVariables.getMessageMap().putError(HoldsConstants.APPLIED_HOLDS_PERSON_ID, HoldsConstants.APPLIED_HOLDS_MSG_ERROR_NO_STUDENT_AFFILIATION, studentId);
94                  return null;
95              }
96              return personInfo;
97          } catch (DoesNotExistException dne) {
98              GlobalVariables.getMessageMap().putError(HoldsConstants.APPLIED_HOLDS_PERSON_ID, HoldsConstants.APPLIED_HOLDS_MSG_ERROR_INVALID_STUDENT, studentId);
99          } catch (Exception e) {
100             throw convertServiceExceptionsToUI(e);
101         }
102 
103         return null;
104     }
105 
106     /**
107      * This method is used to search for holds applied to a person and map them to AppliedHoldResult
108      * Only released holds that have the maintain history option enabled will be displayed.
109      * All active holds will be displayed regardless of the maintain history option.
110      *
111      * @param personId
112      * @return List holdResultList
113      */
114     @Override
115     public List<AppliedHoldResult> searchAppliedHoldsByPerson(String personId) {
116 
117         List<AppliedHoldResult> holdResultList = new ArrayList<AppliedHoldResult>();
118 
119         try {
120             List<AppliedHoldInfo> appliedHoldInfoList = HoldsResourceLoader.getHoldService().getAppliedHoldsByPerson(personId, createContextInfo());
121 
122             for (AppliedHoldInfo appliedHoldInfo : appliedHoldInfoList) {
123 
124                 HoldIssueInfo holdIssue = HoldsResourceLoader.getHoldService().getHoldIssue(appliedHoldInfo.getHoldIssueId(), createContextInfo());
125                 //Check if the hold has the Maintain History Option set to true when the hold is in released state
126                 if (!holdIssue.getMaintainHistoryOfApplicationOfHold() && appliedHoldInfo.getStateKey().matches(HoldServiceConstants.APPLIED_HOLD_EXPIRED_STATE_KEY)) {
127                     continue;
128                 }
129                 if (appliedHoldInfo.getStateKey().matches(HoldServiceConstants.APPLIED_HOLD_DELETED_STATE_KEY)) {
130                     continue;
131                 }
132 
133                 AppliedHoldResult appliedHoldResult = new AppliedHoldResult();
134                 appliedHoldResult.setId(appliedHoldInfo.getId());
135                 appliedHoldResult.setHoldIssue(holdIssue);
136                 appliedHoldResult.setHoldName(holdIssue.getName());
137                 appliedHoldResult.setCode(holdIssue.getHoldCode());
138                 appliedHoldResult.setState(getStateInfo(appliedHoldInfo.getStateKey()).getName());
139                 appliedHoldResult.setTypeKey(holdIssue.getTypeKey());
140                 //Consequence lookup needed
141                 appliedHoldResult.setConsequence("");
142                 appliedHoldResult.setStartDate(appliedHoldInfo.getEffectiveDate());
143                 appliedHoldResult.setEndDate(appliedHoldInfo.getExpirationDate());
144                 appliedHoldResult.setStartTerm(getTermCodeForId(appliedHoldInfo.getApplicationEffectiveTermId()));
145                 appliedHoldResult.setEndTerm(getTermCodeForId(appliedHoldInfo.getApplicationExpirationTermId()));
146 
147                 holdResultList.add(appliedHoldResult);
148             }
149         } catch (Exception e) {
150             convertServiceExceptionsToUI(e);
151         }
152         return holdResultList;
153     }
154 
155     @Override
156     public boolean isAuthorized(String holdIssueId, String function) {
157         return HoldsResourceLoader.getHoldIssueAuthorizingOrgFacade().canPerformFunction(SecurityUtils.getCurrentUserId(),
158                 holdIssueId, function);
159     }
160 
161     @Override
162     public HoldIssueInfo searchHoldIssueByCode(String holdCode) {
163 
164         try {
165             QueryByCriteria query = QueryByCriteria.Builder.fromPredicates(PredicateFactory.and(
166                     PredicateFactory.in(HoldsConstants.HOLD_ISSUE_HOLD_CODE, holdCode)));
167 
168             List<HoldIssueInfo> holdIssueInfos = HoldsResourceLoader.getHoldService().searchForHoldIssues(query, createContextInfo());
169             if (holdIssueInfos.size() == 0 || holdIssueInfos.isEmpty()) {
170                 GlobalVariables.getMessageMap().putError(HoldsConstants.APPLIED_HOLDS_PROP_NAME_CODE, HoldsConstants.APPLIED_HOLDS_MSG_ERROR_HOLD_CODE_INVALID);
171             } else {
172                 return KSCollectionUtils.getOptionalZeroElement(holdIssueInfos);
173             }
174         } catch (Exception e) {
175             GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, RiceKeyConstants.ERROR_CUSTOM, e.getMessage());
176         }
177         return new HoldIssueInfo();
178     }
179 
180     /**
181      * This method is used for the hold code suggest field on the input section on the client.
182      *
183      * @param holdCode
184      * @return List<String> holdIssueCodeList
185      */
186     public List<String> retrieveHoldCodes(String holdCode) {
187 
188         List<String> holdIssueCodeList = new ArrayList<String>();
189 
190         if (holdCode.length() >= 3) {
191             try {
192                 QueryByCriteria.Builder qbcBuilder = QueryByCriteria.Builder.create();
193                 qbcBuilder.setPredicates(PredicateFactory.like(HoldsConstants.HOLD_ISSUE_HOLD_CODE, "%" + holdCode + "%"));
194 
195                 List<HoldIssueInfo> holdIssueInfos = HoldsResourceLoader.getHoldService().searchForHoldIssues(
196                         qbcBuilder.build(), createContextInfo());
197 
198                 for (HoldIssueInfo holdIssueInfo : holdIssueInfos) {
199                     holdIssueCodeList.add(holdIssueInfo.getHoldCode());
200                 }
201             } catch (Exception e) {
202                 convertServiceExceptionsToUI(e);
203             }
204 
205             Collections.sort(holdIssueCodeList);
206         }
207 
208         return holdIssueCodeList;
209     }
210 
211     public String getTermCodeForId(String termId) {
212         if (termId == null) {
213             return StringUtils.EMPTY;
214         }
215 
216         try {
217             TermInfo term = HoldsResourceLoader.getAcademicCalendarService().getTerm(termId, ContextUtils.createDefaultContextInfo());
218             return term.getCode();
219         } catch (Exception e) {
220             convertServiceExceptionsToUI(e);
221         }
222 
223         return StringUtils.EMPTY;
224     }
225 }