View Javadoc

1   package org.kuali.student.lum.bo.options;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import javax.xml.namespace.QName;
7   
8   import org.apache.commons.lang.StringUtils;
9   import org.apache.log4j.Logger;
10  import org.kuali.rice.core.resourceloader.GlobalResourceLoader;
11  import org.kuali.rice.core.util.KeyLabelPair;
12  import org.kuali.rice.kns.lookup.keyvalues.KeyValuesBase;
13  import org.kuali.student.common.search.dto.SearchRequest;
14  import org.kuali.student.common.search.dto.SearchResultCell;
15  import org.kuali.student.common.search.dto.SearchResultRow;
16  import org.kuali.student.core.organization.service.OrganizationService;
17  
18  public class RemoteOrganizationValuesFinder extends KeyValuesBase {
19  
20      private static final Logger LOG = Logger.getLogger(RemoteOrganizationValuesFinder.class);
21      
22      private OrganizationService organizationService;
23      
24      @Override
25      public List<KeyLabelPair> getKeyValues() {
26          List<KeyLabelPair> departments = new ArrayList<KeyLabelPair>();
27  
28          SearchRequest searchRequest = new SearchRequest();
29          searchRequest.setSearchKey("org.search.generic");
30  
31          try {
32              for (SearchResultRow result : getOrganizationService().search(searchRequest).getRows()) {
33                  String orgId = "";
34                  String orgShortName = "";
35                  String orgOptionalLongName = "";
36                  String orgType = "";
37                  for (SearchResultCell resultCell : result.getCells()) {
38                      if ("org.resultColumn.orgId".equals(resultCell.getKey())) {
39                          orgId = resultCell.getValue();
40                      } else if ("org.resultColumn.orgShortName".equals(resultCell.getKey())) {
41                          orgShortName = resultCell.getValue();
42                      } else if ("org.resultColumn.orgOptionalLongName".equals(resultCell.getKey())) {
43                          orgOptionalLongName = resultCell.getValue();
44                      } else if ("org.resultColumn.orgType".equals(resultCell.getKey())) {
45                          orgType = resultCell.getValue();
46                      }
47                  }
48                  departments.add(buildKeyLabelPair(orgId, orgShortName, orgOptionalLongName, orgType));
49              }
50  
51          } catch (Exception e) {
52              LOG.error("Error building KeyValues List", e);
53              // swallow exception for now, in case we want to run without ks up
54              // throw new RuntimeException(e);
55          }
56  
57          return departments;
58      }
59  
60      protected OrganizationService getOrganizationService() {
61          if (organizationService == null) {
62              organizationService = (OrganizationService) GlobalResourceLoader
63                  .getService(new QName("http://student.kuali.org/wsdl/organization","OrganizationService"));
64          }
65          return organizationService;
66      }
67      
68      /**
69       * Builds a valid {@link KeyLabelPair} object for use in Student system KeyValue classes. Will throw an {@link IllegalArgumentException}
70       * if the parameters needed are not passed.
71       * 
72       * @param orgId
73       * @param orgShortName
74       * @param orgLongName
75       * @param orgType
76       * @return
77       */
78      protected KeyLabelPair buildKeyLabelPair(String orgId, String orgShortName, String orgLongName, String orgType) {
79          if (StringUtils.isBlank(orgShortName)) {
80              throw new IllegalArgumentException("Blank value for orgShortName is invalid.");
81          }
82          
83          return new KeyLabelPair(orgId, (StringUtils.isNotBlank(orgLongName) ? orgLongName : orgShortName) );
84      }
85  
86  }