001    /**
002     * Copyright 2005-2011 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.kew.service.KEWServiceLocator;
021    import org.kuali.rice.kew.util.Utilities;
022    import org.kuali.rice.kew.workgroup.GroupId;
023    import org.kuali.rice.kew.workgroup.GroupNameId;
024    import org.kuali.rice.kim.api.group.Group;
025    import org.w3c.dom.Element;
026    import org.w3c.dom.NodeList;
027    
028    /**
029     * Looks up workgroup param to validate workgroup exists and is active.  Returns error message if workgroup is does not exist or is not active.
030     * 
031     * @author Kuali Rice Team (rice.collab@kuali.org)
032     */
033    public class WorkgroupWorkflowEDLConfigComponent extends SimpleWorkflowEDLConfigComponent {
034            
035            private boolean required = false;
036            
037            protected GroupId resolveId(String id) {
038                    String groupName = Utilities.parseGroupName(id);
039            String namespace = Utilities.parseGroupNamespaceCode(id);
040                    return new GroupNameId(namespace, groupName);
041            }
042            
043            @Override
044            public Element getReplacementConfigElement(Element element) {
045                    Element replacementEl = (Element)element.cloneNode(true);
046                    Element type = (Element)((NodeList)replacementEl.getElementsByTagName(EDLXmlUtils.TYPE_E)).item(0);
047                    type.setTextContent("text");
048                    
049                    //find the validation element if required is true set a boolean and determine if blanks
050                    //are allowed based on that
051                    Element validation = (Element)((NodeList)replacementEl.getElementsByTagName(EDLXmlUtils.VALIDATION_E)).item(0);
052                    if (validation != null && validation.getAttribute("required").equals("true")) {
053                            required = true;
054                    }
055                    return replacementEl;
056            }
057            
058            @Override
059            public String getErrorMessage(Element originalConfigElement, RequestParser requestParser, MatchingParam param) {
060                    
061                    if (param.getParamValue().length() == 0 && required == true) {
062                            //empty and required so send error
063                            return ("Workgroup is a required field");
064                    } else if (param.getParamValue().length() == 0 && required == false) { 
065                            //empty but not required then just return 
066                            return null;
067                    }
068                    String wrkgrpName = param.getParamValue();
069                    // TODO add support for namespace here!
070                    Group group = KEWServiceLocator.getIdentityHelperService().getGroup(resolveId(wrkgrpName));
071                    if (group == null) {
072                        return ("Group " + wrkgrpName + " not found.");
073                    }
074                    if (!group.isActive()) {
075                        return ("Group " + wrkgrpName + " is not an active group.");
076                    }
077                    return null;
078            }
079            
080            public boolean isRequired() {
081                    return required;
082            }
083    
084            public void setRequired(boolean required) {
085                    this.required = required;
086            }
087    
088    }