View Javadoc

1   /**
2    * Copyright 2012 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   * Created by bobhurt on 8/7/12
16   */
17  package org.kuali.student.enrollment.kitchensink;
18  
19  import org.hsqldb.lib.StringUtil;
20  import org.kuali.rice.core.api.criteria.InPredicate;
21  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
22  import org.kuali.rice.kim.api.identity.Person;
23  import org.kuali.rice.kim.api.identity.PersonService;
24  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
25  import org.kuali.rice.kim.impl.KIMPropertyConstants;
26  import org.kuali.rice.krad.uif.field.LinkField;
27  import org.kuali.rice.krad.uif.service.impl.ViewHelperServiceImpl;
28  import org.kuali.rice.krad.uif.view.View;
29  import org.kuali.rice.krad.util.BeanPropertyComparator;
30  import org.kuali.rice.krad.util.GlobalVariables;
31  import org.kuali.rice.krad.web.form.UifFormBase;
32  import org.kuali.student.enrollment.acal.dto.KeyDateInfo;
33  import org.kuali.student.enrollment.acal.dto.TermInfo;
34  import org.kuali.student.enrollment.acal.service.AcademicCalendarService;
35  import org.kuali.student.r2.common.dto.ContextInfo;
36  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
37  
38  import javax.xml.namespace.QName;
39  import java.text.MessageFormat;
40  import java.util.ArrayList;
41  import java.util.Arrays;
42  import java.util.Collections;
43  import java.util.HashMap;
44  import java.util.List;
45  import java.util.Map;
46  
47  /**
48   * This class //TODO ...
49   *
50   * @author Kuali Student Team
51   */
52  public class KitchenSinkHelper extends ViewHelperServiceImpl {
53  
54      private AcademicCalendarService academicCalendarService;
55  
56  
57      public TermInfo termInfoAjaxQuery(String termId) {
58          TermInfo termInfo = new TermInfo();
59          if (!StringUtil.isEmpty(termId)) {
60              try {
61                  termInfo = getAcademicCalendarService().getTerm(termId, getContextInfo());
62              }
63              catch (DoesNotExistException e1) {
64                  termInfo.setName("Unknown");
65              }
66              catch (Exception e2) {
67                  e2.printStackTrace();
68              }
69          }
70          return termInfo;
71      }
72  
73      public void delete(int selectedIndex, KitchenSinkForm form) throws Exception{
74          form.getCollection().remove(selectedIndex);
75      }
76  
77      public List<Person> getPersonsForSuggest(String personName) {
78          Map<String, String> searchCriteria = new HashMap<String, String>();
79          searchCriteria.put(KIMPropertyConstants.Person.LAST_NAME, personName+"*");
80          PersonService personService = KimApiServiceLocator.getPersonService();
81          List<Person> personList = personService.findPeople(searchCriteria);
82  
83          // sort results, as property "sortPropertyNames" is ignored when "queryMethodToCall" is used
84          if ((personList != null) && (personList.size() > 1)) {
85              Collections.sort((List<?>) personList, new BeanPropertyComparator(Arrays.asList("lastName","firstName")));//attributeQuery.getSortPropertyNames()));
86          }
87  
88          return personList;
89      }
90  
91      public void setSourceLinkText(LinkField linkField, Object model) {
92          linkField.setLinkText(linkField.getHref());
93      }
94  
95      public void setDirectLinkUrl(LinkField linkField, Object model, String methodToCall) {
96          if (StringUtil.isEmpty(methodToCall)) {
97              methodToCall = "start";
98          }
99  
100         String url = MessageFormat.format("{0}?viewId={1}&amp;methodToCall={2}",
101                 ((KitchenSinkForm) model).getFormPostUrl(),
102                 ((KitchenSinkForm) model).getViewId() + "-Bare", methodToCall);
103         linkField.setLinkText(url);
104         linkField.setHref(url);
105     }
106     public void setDirectLinkUrl(LinkField linkField, Object model) {
107         setDirectLinkUrl(linkField, model, null);
108     }
109 
110     @Override
111     public void processCollectionSaveLine(View view, Object model, String collectionPath, int lineIndex) {
112         //
113         // Code goes here to save existing collection line to database...
114         //
115 
116         if (view.getId().startsWith("KS-KitchenSink-CollectionAsForm-View")) {
117             // only set growl for the Collection As Form example
118             GlobalVariables.getMessageMap().addGrowlMessage("NOTE", "kitchensink.saveLine", String.valueOf(lineIndex));
119         }
120         super.processCollectionSaveLine(view, model, collectionPath, lineIndex);
121     }
122 
123     @Override
124     public void processCollectionDeleteLine(View view, Object model, String collectionPath, int lineIndex) {
125         //
126         // Code goes here to delete existing collection line from database...
127         //
128 
129         if (view.getId().startsWith("KS-KitchenSink-CollectionAsForm-View")) {
130             // only set growl for the Collection As Form example
131             GlobalVariables.getMessageMap().addGrowlMessage("NOTE", "kitchensink.deleteLine", String.valueOf(lineIndex));
132         }
133         super.processCollectionDeleteLine(view, model, collectionPath, lineIndex);
134     }
135 
136 
137 
138     private AcademicCalendarService getAcademicCalendarService() {
139         if(academicCalendarService == null) {
140             academicCalendarService = (AcademicCalendarService) GlobalResourceLoader.getService(new QName("http://student.kuali.org/wsdl/acal", "AcademicCalendarService"));
141         }
142         return academicCalendarService;
143     }
144 
145     // TODO - where does context come from?
146     private ContextInfo getContextInfo() {
147         return new ContextInfo();
148     }
149 
150 }