View Javadoc

1   package org.kuali.student.enrollment.class1.hold.service.impl;
2   
3   import org.kuali.student.r2.common.constants.CommonServiceConstants;
4   import org.kuali.student.r2.common.dto.AttributeInfo;
5   import org.kuali.student.r2.common.dto.ContextInfo;
6   import org.kuali.student.r2.common.dto.RichTextInfo;
7   import org.kuali.student.r2.common.exceptions.AlreadyExistsException;
8   import org.kuali.student.r2.common.exceptions.CircularRelationshipException;
9   import org.kuali.student.r2.common.exceptions.DataValidationErrorException;
10  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
11  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
12  import org.kuali.student.r2.common.exceptions.MissingParameterException;
13  import org.kuali.student.r2.common.exceptions.OperationFailedException;
14  import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
15  import org.kuali.student.r2.common.exceptions.ReadOnlyException;
16  import org.kuali.student.r2.common.exceptions.VersionMismatchException;
17  import org.kuali.student.r2.common.util.date.DateFormatters;
18  import org.kuali.student.r2.core.hold.dto.AppliedHoldInfo;
19  import org.kuali.student.r2.core.hold.dto.HoldIssueInfo;
20  import org.kuali.student.r2.core.hold.service.HoldService;
21  
22  import java.util.ArrayList;
23  import java.util.Date;
24  import java.util.List;
25  
26  public class HoldTestDataLoader {
27  
28      public HoldTestDataLoader() {
29      }
30  
31      public HoldTestDataLoader(HoldService holdService) {
32          this.holdService = holdService;
33      }
34  
35      public HoldService getHoldService() {
36          return holdService;
37      }
38  
39      public void setHoldService(HoldService holdService) {
40          this.holdService = holdService;
41      }
42  
43      private HoldService holdService;
44      private static String principalId = HoldTestDataLoader.class.getSimpleName();
45      private ContextInfo context;
46  
47      public void loadData() throws DoesNotExistException, InvalidParameterException,
48              MissingParameterException, OperationFailedException, PermissionDeniedException,
49              DataValidationErrorException, ReadOnlyException, VersionMismatchException,
50              AlreadyExistsException, CircularRelationshipException {
51  
52          context = new ContextInfo();
53          context.setPrincipalId(principalId);
54          context.setCurrentDate(new Date());
55          CommonServiceConstants.setIsIdAllowedOnCreate(context, true);
56  
57          loadHoldIssue(HoldTestConstants.HOLD_ISSUE_1, "Test issue 1", HoldTestConstants.ISSUE_TYPE, HoldTestConstants.DRAFT_STATE, "Formatted", "Plain", HoldTestConstants.ORG_1);
58          loadHoldIssue(HoldTestConstants.HOLD_ISSUE_2, "Test issue 2", HoldTestConstants.ISSUE_TYPE, HoldTestConstants.DRAFT_STATE, "Formatted", "Plain", HoldTestConstants.ORG_1);
59          loadHoldIssue(HoldTestConstants.HOLD_ISSUE_3, "Test issue 3", HoldTestConstants.ISSUE_TYPE, HoldTestConstants.DRAFT_STATE, "Formatted", "Plain", HoldTestConstants.ORG_2);
60          loadHoldIssue(HoldTestConstants.HOLD_ISSUE_4, "Test issue 4", HoldTestConstants.ISSUE_TYPE, HoldTestConstants.DRAFT_STATE, "Formatted", "Plain", HoldTestConstants.ORG_2);
61  
62          //holds can be added here later as required for testing
63          //loadHold("kuali.hold.type.financialHold","kuali.hold.state.draft","Formatted","Plain","HOLDISSUE-1","testFakePerson1",str2Date("2011-01-01 00:00:00.0",context), new Date());
64      }
65  
66      private void loadHoldIssue(String id, String name, String typeKey, String stateKey, String descrFormatted, String descrPlain, String orgID) throws InvalidParameterException, DataValidationErrorException, MissingParameterException, AlreadyExistsException, ReadOnlyException, PermissionDeniedException, OperationFailedException {
67          HoldIssueInfo issueInfo = new HoldIssueInfo();
68          issueInfo.setId(id);
69          issueInfo.setStateKey(stateKey);
70          issueInfo.setTypeKey(typeKey);
71          issueInfo.setOrganizationId(orgID);
72          issueInfo.setDescr(new RichTextInfo(descrPlain, descrFormatted));
73          issueInfo.setName(name);
74          holdService.createHoldIssue(issueInfo.getTypeKey(), issueInfo, context);
75      }
76  
77      private void loadHold(String typeKey, String stateKey, String descrFormatted, String descrPlain, String issueID, String personID, Date effectiveDate, Date releaseDate) throws InvalidParameterException, DataValidationErrorException, MissingParameterException, AlreadyExistsException, ReadOnlyException, PermissionDeniedException, OperationFailedException {
78          AppliedHoldInfo holdInfo = new AppliedHoldInfo();
79          holdInfo.setTypeKey(typeKey);
80          holdInfo.setStateKey(stateKey);
81          holdInfo.setDescr(new RichTextInfo(descrPlain, descrFormatted));
82          holdInfo.setHoldIssueId(issueID);
83          holdInfo.setPersonId(personID);
84          holdInfo.setEffectiveDate(effectiveDate);
85          holdInfo.setReleasedDate(releaseDate);
86          holdService.createAppliedHold(personID, issueID, typeKey, holdInfo, context);
87      }
88  
89      public List<AttributeInfo> createAttributes() {
90          List<AttributeInfo> attributes = new ArrayList<AttributeInfo>();
91          attributes.add((new AttributeInfo(HoldTestConstants.TEST_KEY_2, HoldTestConstants.TEST_VALUE_2)));
92          attributes.add((new AttributeInfo(HoldTestConstants.TEST_KEY_2, HoldTestConstants.TEST_VALUE_2)));
93          return attributes;
94      }
95  
96      private Date str2Date(String str, String context) {
97          if (str == null) {
98              return null;
99          }
100         try {
101             Date date = DateFormatters.DEFAULT_YEAR_MONTH_24HOUR_MILLISECONDS_FORMATTER.parse(str);
102             return date;
103         } catch (IllegalArgumentException ex) {
104             throw new IllegalArgumentException("Bad date " + str + " in " + context);
105         }
106     }
107 }