001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.edl.impl.components;
017    
018    import org.kuali.rice.edl.impl.EDLXmlUtils;
019    import org.kuali.rice.edl.impl.RequestParser;
020    import org.kuali.rice.kim.api.identity.principal.Principal;
021    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
022    import org.w3c.dom.Element;
023    import org.w3c.dom.NodeList;
024    
025    
026    /**
027     * Matches network ID param to UserService to validate network Id.  Returns error message if networkId does NOT match.
028     * 
029     * @author Kuali Rice Team (rice.collab@kuali.org)
030     */
031    public class NetworkIdWorkflowEDLConfigComponent extends SimpleWorkflowEDLConfigComponent {
032            
033            private boolean required = false;
034            
035            @Override
036            public Element getReplacementConfigElement(Element element) {
037                    Element replacementEl = (Element)element.cloneNode(true);
038                    Element type = (Element)((NodeList)replacementEl.getElementsByTagName(EDLXmlUtils.TYPE_E)).item(0);
039                    type.setTextContent("text");
040                    
041                    //find the validation element if required is true set a boolean and determin if blanks
042                    //are allowed based on that
043                    Element validation = (Element)((NodeList)replacementEl.getElementsByTagName(EDLXmlUtils.VALIDATION_E)).item(0);
044                    if (validation != null && validation.getAttribute("required").equals("true")) {
045                            required = true;
046                    }
047                    return replacementEl;
048            }
049            
050            @Override
051            public String getErrorMessage(Element originalConfigElement, RequestParser requestParser, MatchingParam param) {
052            if (!getEdlContext().getUserAction().isValidatableAction()) {
053                return null;
054            } else if (param.getParamValue().length() == 0 && required == true) {
055                            //empty and required so send error
056                            return ("Network ID is a required field");
057                    } else if (param.getParamValue().length() == 0 && required == false) { 
058                            //empty but not required then just return 
059                            return null;                    
060                    } else {
061                            //not blank validate as normal whether required or not
062                            Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(param.getParamValue());
063                            if (principal == null) {
064                                    return ("The value " + param.getParamValue() + " is an invalid principal name");
065                            }
066                    }
067                    return null;
068            }
069    
070            public boolean isRequired() {
071                    return required;
072            }
073    
074            public void setRequired(boolean required) {
075                    this.required = required;
076            }
077    
078    }