001/**
002 * Copyright 2005-2014 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 */
016package org.kuali.rice.edl.impl.components;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.edl.impl.EDLXmlUtils;
020import org.kuali.rice.edl.impl.RequestParser;
021import org.kuali.rice.kim.api.identity.principal.Principal;
022import org.kuali.rice.kim.api.services.KimApiServiceLocator;
023import org.w3c.dom.Element;
024import org.w3c.dom.NodeList;
025
026
027/**
028 * Matches network ID param to UserService to validate network Id.  Returns error message if networkId does NOT match.
029 * 
030 * @author Kuali Rice Team (rice.collab@kuali.org)
031 */
032public class NetworkIdWorkflowEDLConfigComponent extends SimpleWorkflowEDLConfigComponent {
033        
034        private boolean required = false;
035        
036        @Override
037        public Element getReplacementConfigElement(Element element) {
038                Element replacementEl = (Element)element.cloneNode(true);
039                Element type = (Element)((NodeList)replacementEl.getElementsByTagName(EDLXmlUtils.TYPE_E)).item(0);
040                type.setTextContent("text");
041                
042                //find the validation element if required is true set a boolean and determin if blanks
043                //are allowed based on that
044                Element validation = (Element)((NodeList)replacementEl.getElementsByTagName(EDLXmlUtils.VALIDATION_E)).item(0);
045                if (validation != null && validation.getAttribute("required").equals("true")) {
046                        required = true;
047                }
048                return replacementEl;
049        }
050        
051        @Override
052        public String getErrorMessage(Element originalConfigElement, RequestParser requestParser, MatchingParam param) {
053        if (!getEdlContext().getUserAction().isValidatableAction()) {
054            return null;
055        } else if (StringUtils.isBlank(param.getParamValue()) && required ) {
056            // empty or whitespaces and required so send error
057            return ("Network ID is a required field");
058        } else if (StringUtils.isBlank(param.getParamValue()) && !required ) {
059            // empty or whitespaces and not required so ignore
060            return null;
061                } else {
062                        //not blank validate as normal whether required or not
063                        Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(param.getParamValue());
064                        if (principal == null) {
065                                return ("The value " + param.getParamValue() + " is an invalid principal name");
066                        }
067                }
068                return null;
069        }
070
071        public boolean isRequired() {
072                return required;
073        }
074
075        public void setRequired(boolean required) {
076                this.required = required;
077        }
078
079}