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