View Javadoc

1   /**
2    * Copyright 2011 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.student.enrollment.class2.acal.util;
17  
18  import org.kuali.student.r2.common.dto.ContextInfo;
19  import org.kuali.student.r2.common.util.RichTextHelper;
20  import org.kuali.student.r2.core.acal.dto.TermInfo;
21  import org.kuali.student.r2.core.acal.service.AcademicCalendarService;
22  import org.kuali.student.r2.core.constants.AtpServiceConstants;
23  
24  import java.text.DateFormat;
25  import java.text.ParseException;
26  import java.text.SimpleDateFormat;
27  import java.util.Date;
28  
29  /**
30   * @author andrewlubbers
31   * This Academic Calendar data loader does not require an instance of the Atp service.
32   * Intended for use with mock Acal service implementations for testing
33   */
34  public class MockAcalTestDataLoader {
35  
36      private AcademicCalendarService acalService;
37      private static String principalId = MockAcalTestDataLoader.class.getSimpleName();
38  
39      public AcademicCalendarService getAcalService() {
40          return acalService;
41      }
42  
43      public void setAcalService(AcademicCalendarService acalService) {
44          this.acalService = acalService;
45      }
46  
47      public MockAcalTestDataLoader(AcademicCalendarService acalService) {
48          this.acalService = acalService;
49      }
50  
51      public void loadData() {
52          loadTerm("testAtpId1", "test1", "2000-01-01 00:00:00.0", "2100-12-31 00:00:00.0", AtpServiceConstants.ATP_FALL_TYPE_KEY, AtpServiceConstants.ATP_OFFICIAL_STATE_KEY, "description 1");
53      }
54  
55      public TermInfo loadTerm(String id,
56                               String name,
57                               String startDate, String endDate, String type, String state, String description) {
58  
59          TermInfo info = new TermInfo();
60          info.setId(id);
61          info.setCode(id);// use id for code
62          info.setName(name);
63          info.setDescr(new RichTextHelper().fromPlain(description));
64          info.setTypeKey(type);
65          info.setStateKey(state);
66          info.setStartDate(str2Date(startDate, id));
67          info.setEndDate(str2Date(endDate, id));
68  
69  
70          ContextInfo context = new ContextInfo();
71          context.setPrincipalId(principalId);
72          context.setCurrentDate(new Date());
73          try {
74              return this.acalService.createTerm(type, info, context);
75          } catch (Exception ex) {
76              throw new RuntimeException(ex);
77          }
78      }
79  
80      private Date str2Date(String str, String context) {
81          if (str == null) {
82              return null;
83          }
84          DateFormat df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss.S");
85          try {
86              return df.parse(str);
87          } catch (ParseException ex) {
88              throw new IllegalArgumentException("Bad date " + str + " in " + context);
89          }
90      }
91  
92  }