View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.coreservice.web.component;
17  
18  import org.apache.commons.collections.CollectionUtils;
19  import org.apache.commons.lang.StringUtils;
20  import org.kuali.rice.coreservice.impl.component.ComponentBo;
21  import org.kuali.rice.coreservice.impl.component.DerivedComponentBo;
22  import org.kuali.rice.kns.lookup.HtmlData;
23  import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
24  import org.kuali.rice.krad.bo.BusinessObject;
25  import org.kuali.rice.krad.lookup.CollectionIncomplete;
26  import org.kuali.rice.krad.lookup.LookupUtils;
27  import org.kuali.rice.krad.service.KRADServiceLocator;
28  
29  import java.util.Collection;
30  import java.util.List;
31  
32  public class ComponentLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
33  
34      private static final long serialVersionUID = -3978422770535345525L;
35  
36      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ComponentLookupableHelperServiceImpl.class);
37      
38      private static final String ACTIVE = "active";
39      private static final String CODE = "code";
40      private static final String NAMESPACE_CODE = "namespaceCode";
41      private static final String NAME = "name";
42  
43      @Override
44      public List<? extends BusinessObject> getSearchResults(java.util.Map<String, String> fieldValues) {
45  
46          List<BusinessObject> baseLookup = (List<BusinessObject>) super.getSearchResults(fieldValues);
47  
48          String activeCheck = fieldValues.get(ACTIVE);
49          if (activeCheck == null) {
50              activeCheck = "";
51          }
52          int maxResultsCount = LookupUtils.getSearchResultsLimit(ComponentBo.class);
53          // only bother with the component lookup if returning active components
54          if (baseLookup instanceof CollectionIncomplete && !activeCheck.equals("N")) {
55              long originalCount = Math.max(baseLookup.size(), ((CollectionIncomplete) baseLookup).getActualSizeIfTruncated());
56              long totalCount = originalCount;
57  
58              Collection<DerivedComponentBo> derivedComponentBos = null;
59              if (StringUtils.isBlank(fieldValues.get(CODE)) && StringUtils.isBlank(fieldValues.get(NAMESPACE_CODE))
60                      && StringUtils.isBlank(fieldValues.get(NAME))) {
61                  derivedComponentBos = KRADServiceLocator.getBusinessObjectService().findAll(DerivedComponentBo.class);
62              } else {
63                  derivedComponentBos = getLookupService().findCollectionBySearchHelper(DerivedComponentBo.class, fieldValues, false);
64              }
65              if (CollectionUtils.isNotEmpty(derivedComponentBos)) {
66                  for (DerivedComponentBo derivedComponentBo : derivedComponentBos) {
67                      if (totalCount++ < maxResultsCount) {
68                          baseLookup.add(DerivedComponentBo.toComponentBo(derivedComponentBo));
69                      } else {
70                          break;
71                      }
72                  }
73              }
74  
75              if (totalCount > maxResultsCount) {
76                  ((CollectionIncomplete) baseLookup).setActualSizeIfTruncated(totalCount);
77              }
78              else {
79                  ((CollectionIncomplete) baseLookup).setActualSizeIfTruncated(0L);
80              }
81          }
82  
83          return baseLookup;
84      }
85  
86      /**
87       * Suppress the edit/copy links on synthetic detail types.
88       */
89      @Override
90      public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
91          if ( ((ComponentBo)businessObject).getObjectId() == null ) {
92              return super.getEmptyActionUrls();
93          }
94          return super.getCustomActionUrls(businessObject, pkNames);
95      }
96  
97  }