View Javadoc

1   /**
2    * Copyright 2012 The Kuali Foundation Licensed under the Educational Community License, Version 2.0 (the "License"); you may not
3    * use this file except in compliance with the License. You may obtain a copy of the License at
4    *
5    * http://www.osedu.org/licenses/ECL-2.0
6    *
7    * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
8    * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
9    * governing permissions and limitations under the License.
10   *
11   * Created by Daniel on 4/26/12
12   */
13  package org.kuali.student.r2.common.class1.search;
14  
15  import org.kuali.student.r2.common.dto.ContextInfo;
16  import org.kuali.student.r2.common.exceptions.MissingParameterException;
17  import org.kuali.student.r2.common.exceptions.OperationFailedException;
18  import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
19  import org.kuali.student.r2.common.util.RichTextHelper;
20  import org.kuali.student.r2.core.class1.type.dto.TypeInfo;
21  import org.kuali.student.r2.core.search.dto.SearchRequestInfo;
22  import org.kuali.student.r2.core.search.dto.SearchResultInfo;
23  import org.kuali.student.r2.core.search.util.SearchRequestHelper;
24  import org.kuali.student.r2.core.search.util.SearchResultCreatorHelper;
25  
26  import javax.persistence.Query;
27  import java.text.DateFormat;
28  import java.text.ParseException;
29  import java.text.SimpleDateFormat;
30  
31  /**
32   * This class implements the search service by hardwiring in the JPQL to get appointment window counts
33   *
34   * @author Kuali Student Team
35   */
36  public class ApptWindowCountsSearchImpl
37          extends SearchServiceAbstractHardwiredImpl {
38  
39      public static final String CREATE_TIME = "createTime";
40      public static final String FIRST_SLOT = "firstSlot";
41      public static final String LAST_SLOT = "lastSlot";
42      public static final String NUM_APPTS = "numAppts";
43      public static final String NUM_SLOTS = "numSlots";
44      
45      /**
46       * The search type for this search
47       */
48      public static final TypeInfo SEARCH_TYPE;
49  
50      static {
51          TypeInfo info = new TypeInfo();
52          info.setKey("kuali.search.type.appt.appointmentCountsForWindowId");
53          info.setName("Appointment Counts for Window Id");
54          info.setDescr(new RichTextHelper().fromPlain("Gets summary counts fo slots and appointmetns for an appointment window"));
55          DateFormat mmddyyyy = new SimpleDateFormat("MM/dd/yyyy");
56          try {
57              info.setEffectiveDate(mmddyyyy.parse("01/01/2012"));
58          } catch (ParseException ex) {
59              throw new RuntimeException("bad code");
60          }
61          SEARCH_TYPE = info;
62      }
63      /**
64       * The appointment window id parameter type
65       */
66      public static final TypeInfo APPT_WINDOW_ID;
67  
68      static {
69          TypeInfo info = new TypeInfo();
70          info.setKey("windowId");
71          info.setName("Appointment Window Id");
72          info.setDescr(new RichTextHelper().fromPlain("The appointment window id for which you want to get the counts"));
73          DateFormat mmddyyyy = new SimpleDateFormat("MM/dd/yyyy");
74          try {
75              info.setEffectiveDate(mmddyyyy.parse("01/01/2012"));
76          } catch (ParseException ex) {
77              throw new RuntimeException("bad code");
78          }
79          APPT_WINDOW_ID = info;
80      }
81      /**
82       * The search parameters used by this search
83       */
84      public static final TypeInfo[] SEARCH_PARAMETERS = {APPT_WINDOW_ID};
85  
86      @Override
87      public TypeInfo getSearchType() {
88          return SEARCH_TYPE;
89      }
90  
91      @Override
92      public SearchResultInfo search(SearchRequestInfo searchRequestInfo, ContextInfo contextInfo)
93              throws MissingParameterException,
94              OperationFailedException,
95              PermissionDeniedException {
96          if (!searchRequestInfo.getSearchKey().equals(SEARCH_TYPE.getKey())) {
97              throw new OperationFailedException("Unsupported search type: " + searchRequestInfo.getSearchKey());
98          }
99          SearchRequestHelper requestHelper = new SearchRequestHelper(searchRequestInfo);
100         //This is a hardwired search for AppointmentWindows
101         //It gets the count of appointments for a given window Id
102         String windowId = requestHelper.getParamAsString(APPT_WINDOW_ID.getKey());
103         SearchResultCreatorHelper creatorHelper = new SearchResultCreatorHelper();
104         creatorHelper.getHeaders().add(CREATE_TIME);
105         creatorHelper.getHeaders().add(FIRST_SLOT);
106         creatorHelper.getHeaders().add(LAST_SLOT);
107         creatorHelper.getHeaders().add(NUM_APPTS);
108         creatorHelper.getHeaders().add(NUM_SLOTS);
109         Query query = this.getGenericEntityDao().getEm().createNamedQuery("AppointmentWindowEntity.appointmentWindowCounts");
110         query.setParameter("windowId", windowId);
111         creatorHelper.processQuery(query);
112         return creatorHelper.getSearchResult();
113     }
114 }