View Javadoc

1   /*
2    * Copyright 2006-2011 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.core.web.component;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.component.Component;
20  import org.kuali.rice.core.framework.parameter.ParameterService;
21  import org.kuali.rice.core.impl.component.ComponentBo;
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.datadictionary.DataDictionaryException;
26  import org.kuali.rice.krad.lookup.CollectionIncomplete;
27  import org.kuali.rice.krad.lookup.LookupUtils;
28  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
29  
30  import java.util.List;
31  import java.util.regex.Pattern;
32  import java.util.regex.PatternSyntaxException;
33  
34  public class ComponentLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
35  
36      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ComponentLookupableHelperServiceImpl.class);
37      private static final String ACTIVE = "active";
38      private static final String CODE = "code";
39      private static final String NAMESPACE_CODE = "namespaceCode";
40      private static final String NAME = "name";
41      private ParameterService parameterService;
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          // all step beans
49          // all BO beans
50          // all trans doc beans
51  
52          List<Component> components;
53          try {
54          	components = KRADServiceLocatorWeb.getRiceApplicationConfigurationMediationService().getNonDatabaseComponents();
55          }
56          catch (DataDictionaryException ex) {
57              throw new RuntimeException("Problem parsing data dictionary during full load required for lookup to function: " + ex.getMessage(), ex);
58          }
59  
60          String activeCheck = fieldValues.get(ACTIVE);
61          if (activeCheck == null) {
62              activeCheck = "";
63          }
64          int maxResultsCount = LookupUtils.getSearchResultsLimit(ComponentBo.class);
65          // only bother with the component lookup if returning active components
66          if (baseLookup instanceof CollectionIncomplete && !activeCheck.equals("N")) {
67              long originalCount = Math.max(baseLookup.size(), ((CollectionIncomplete) baseLookup).getActualSizeIfTruncated());
68              long totalCount = originalCount;
69              Pattern detailTypeRegex = null;
70              Pattern namespaceRegex = null;
71              Pattern nameRegex = null;
72  
73              if (StringUtils.isNotBlank(fieldValues.get(CODE))) {
74                  String patternStr = fieldValues.get(CODE).replace("*", ".*").toUpperCase();
75                  try {
76                      detailTypeRegex = Pattern.compile(patternStr);
77                  }
78                  catch (PatternSyntaxException ex) {
79                      LOG.error("Unable to parse code pattern, ignoring.", ex);
80                  }
81              }
82              if (StringUtils.isNotBlank(fieldValues.get(NAMESPACE_CODE))) {
83                  String patternStr = fieldValues.get(NAMESPACE_CODE).replace("*", ".*").toUpperCase();
84                  try {
85                      namespaceRegex = Pattern.compile(patternStr);
86                  }
87                  catch (PatternSyntaxException ex) {
88                      LOG.error("Unable to parse namespaceCode pattern, ignoring.", ex);
89                  }
90              }
91              if (StringUtils.isNotBlank(fieldValues.get(NAME))) {
92                  String patternStr = fieldValues.get(NAME).replace("*", ".*").toUpperCase();
93                  try {
94                      nameRegex = Pattern.compile(patternStr);
95                  }
96                  catch (PatternSyntaxException ex) {
97                      LOG.error("Unable to parse name pattern, ignoring.", ex);
98                  }
99              }
100             for (Component pdt : components) {
101                 boolean includeType = true;
102                 if (detailTypeRegex != null) {
103                     includeType = detailTypeRegex.matcher(pdt.getCode().toUpperCase()).matches();
104                 }
105                 if (includeType && namespaceRegex != null) {
106                     includeType = namespaceRegex.matcher(pdt.getNamespaceCode().toUpperCase()).matches();
107                 }
108                 if (includeType && nameRegex != null) {
109                     includeType = nameRegex.matcher(pdt.getName().toUpperCase()).matches();
110                 }
111                 if (includeType) {
112                     if (totalCount < maxResultsCount) {
113                         baseLookup.add(ComponentBo.from(pdt));
114                     }
115                     totalCount++;
116                 }
117             }
118             if (totalCount > maxResultsCount) {
119                 ((CollectionIncomplete) baseLookup).setActualSizeIfTruncated(totalCount);
120             }
121             else {
122                 ((CollectionIncomplete) baseLookup).setActualSizeIfTruncated(0L);
123             }
124         }
125 
126         return baseLookup;
127     }
128 
129     /**
130      * Suppress the edit/copy links on synthetic detail types.
131      * 
132      * @see org.kuali.rice.krad.lookup.AbstractLookupableHelperServiceImpl#getCustomActionUrls(org.kuali.rice.krad.bo.BusinessObject, java.util.List)
133      */
134     @Override
135     public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
136         if ( ((ComponentBo)businessObject).getObjectId() == null ) {
137             return super.getEmptyActionUrls();
138         }
139         return super.getCustomActionUrls(businessObject, pkNames);
140     }
141 
142     public void setParameterService(ParameterService parameterService) {
143         this.parameterService = parameterService;
144     }
145 
146 }