View Javadoc
1   /**
2    * Copyright 2005-2014 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.krms.impl.ui;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
20  import org.kuali.rice.krms.api.repository.operator.CustomOperator;
21  import org.kuali.rice.krms.impl.util.KrmsImplConstants;
22  
23  import javax.xml.namespace.QName;
24  
25  /**
26   * Utility service used by the KRMS agenda editing UI for display and access to custom operators.
27   *
28   * @author Kuali Rice Team (rice.collab@kuali.org)
29   */
30  public class CustomOperatorUiTranslator {
31  
32  
33      /**
34       * Parses the {@link QName} for the custom operator service from the form value string, which has the format
35       * {@code customOperator:<Namespace>:<serviceName>}
36       *
37       * @param customOperatorFormValue
38       * @return the QName for the custom operator service
39       */
40      public QName parseCustomOperatorServiceQName(String customOperatorFormValue) {
41          if (customOperatorFormValue == null ||
42                  !isCustomOperatorFormValue(customOperatorFormValue)) {
43              throw new IllegalArgumentException("custom operator form value (" + customOperatorFormValue +
44                      ") must be formatted as " +
45                      KrmsImplConstants.CUSTOM_OPERATOR_PREFIX + "namespace:serviceName");
46          }
47  
48          String [] customOperatorServiceInfo = customOperatorFormValue.split(":");
49  
50          if (customOperatorServiceInfo == null || customOperatorServiceInfo.length != 3) {
51              throw new IllegalArgumentException("custom operator form value (" + customOperatorFormValue +
52                      ") must be formatted as " +
53                      KrmsImplConstants.CUSTOM_OPERATOR_PREFIX + "namespace:serviceName");
54          }
55  
56          QName customOperatorServiceQName = new QName(customOperatorServiceInfo[1], customOperatorServiceInfo[2]);
57  
58          return customOperatorServiceQName;
59      }
60  
61      /**
62       * Gets the custom operator function name which is used for display purposes
63       *
64       * @param customOperatorFormValue the form value representing the custom operator
65       * @return
66       */
67      public String getCustomOperatorName(String customOperatorFormValue) {
68          return getCustomOperator(customOperatorFormValue).getOperatorFunctionDefinition().getName();
69      }
70  
71      /**
72       * Gets the service instance given the specially formatted form value.
73       *
74       * <p>The form value contains the namespace and name of the service, which is used internally for retrieval.</p>
75       *
76       * @param customOperatorFormValue the custom operator form value
77       * @return the custom operator service instance
78       * @throws IllegalArgumentException if the customOperatorFormValue is null or is formatted incorrectly
79       */
80      public CustomOperator getCustomOperator(String customOperatorFormValue) {
81          return GlobalResourceLoader.getService(parseCustomOperatorServiceQName(customOperatorFormValue));
82      }
83  
84      /**
85       * Checks if a form value represents a custom operator.
86       *
87       * <p>The determination is made be checking for a special prefix value that is used by convention.</p>
88       *
89       * @param formValue the form value to check
90       * @return true if the form value represents a custom operator.
91       */
92      public boolean isCustomOperatorFormValue(String formValue) {
93          if (StringUtils.isEmpty(formValue)) {
94              return false;
95          }
96  
97          return formValue.startsWith(KrmsImplConstants.CUSTOM_OPERATOR_PREFIX);
98      }
99  
100 }