View Javadoc
1   /**
2    * Copyright 2005-2015 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.edl.impl.components;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.edl.impl.EDLXmlUtils;
20  import org.kuali.rice.edl.impl.RequestParser;
21  import org.kuali.rice.kim.api.identity.principal.Principal;
22  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
23  import org.w3c.dom.Element;
24  import org.w3c.dom.NodeList;
25  
26  
27  /**
28   * Matches network ID param to UserService to validate network Id.  Returns error message if networkId does NOT match.
29   * 
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   */
32  public class NetworkIdWorkflowEDLConfigComponent extends SimpleWorkflowEDLConfigComponent {
33  	
34  	private boolean required = false;
35  	
36  	@Override
37  	public Element getReplacementConfigElement(Element element) {
38  		Element replacementEl = (Element)element.cloneNode(true);
39  		Element type = (Element)((NodeList)replacementEl.getElementsByTagName(EDLXmlUtils.TYPE_E)).item(0);
40  		type.setTextContent("text");
41  		
42  		//find the validation element if required is true set a boolean and determin if blanks
43  		//are allowed based on that
44  		Element validation = (Element)((NodeList)replacementEl.getElementsByTagName(EDLXmlUtils.VALIDATION_E)).item(0);
45  		if (validation != null && validation.getAttribute("required").equals("true")) {
46  			required = true;
47  		}
48  		return replacementEl;
49  	}
50  	
51  	@Override
52  	public String getErrorMessage(Element originalConfigElement, RequestParser requestParser, MatchingParam param) {
53          if (!getEdlContext().getUserAction().isValidatableAction()) {
54              return null;
55          } else if (StringUtils.isBlank(param.getParamValue()) && required ) {
56              // empty or whitespaces and required so send error
57              return ("Network ID is a required field");
58          } else if (StringUtils.isBlank(param.getParamValue()) && !required ) {
59              // empty or whitespaces and not required so ignore
60              return null;
61  		} else {
62  			//not blank validate as normal whether required or not
63  			Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(param.getParamValue());
64  			if (principal == null) {
65  				return ("The value " + param.getParamValue() + " is an invalid principal name");
66  			}
67  		}
68  		return null;
69  	}
70  
71  	public boolean isRequired() {
72  		return required;
73  	}
74  
75  	public void setRequired(boolean required) {
76  		this.required = required;
77  	}
78  
79  }