001/**
002 * Copyright 2005-2018 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.kew.api.rule;
017
018import java.io.Serializable;
019import java.util.Collection;
020import javax.xml.bind.annotation.XmlAccessType;
021import javax.xml.bind.annotation.XmlAccessorType;
022import javax.xml.bind.annotation.XmlAnyElement;
023import javax.xml.bind.annotation.XmlElement;
024import javax.xml.bind.annotation.XmlRootElement;
025import javax.xml.bind.annotation.XmlType;
026
027import org.apache.commons.lang.StringUtils;
028import org.kuali.rice.core.api.CoreConstants;
029import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
030import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
031import org.kuali.rice.core.api.mo.ModelBuilder;
032import org.w3c.dom.Element;
033
034@XmlRootElement(name = RoleName.Constants.ROOT_ELEMENT_NAME)
035@XmlAccessorType(XmlAccessType.NONE)
036@XmlType(name = RoleName.Constants.TYPE_NAME, propOrder = {
037    RoleName.Elements.NAME,
038    RoleName.Elements.BASE_NAME,
039    RoleName.Elements.RETURN_URL,
040    RoleName.Elements.LABEL,
041    CoreConstants.CommonElements.FUTURE_ELEMENTS
042})
043public final class RoleName
044    extends AbstractDataTransferObject
045    implements RoleNameContract
046{
047
048    @XmlElement(name = Elements.NAME, required = true)
049    private final String name;
050    @XmlElement(name = Elements.BASE_NAME, required = true)
051    private final String baseName;
052    @XmlElement(name = Elements.RETURN_URL, required = false)
053    private final String returnUrl;
054    @XmlElement(name = Elements.LABEL, required = true)
055    private final String label;
056    @SuppressWarnings("unused")
057    @XmlAnyElement
058    private final Collection<Element> _futureElements = null;
059
060    /**
061     * Private constructor used only by JAXB.
062     *
063     */
064    private RoleName() {
065        this.name = null;
066        this.baseName = null;
067        this.returnUrl = null;
068        this.label = null;
069    }
070
071    private RoleName(Builder builder) {
072        this.name = builder.getName();
073        this.baseName = builder.getBaseName();
074        this.returnUrl = builder.getReturnUrl();
075        this.label = builder.getLabel();
076    }
077
078    public RoleName(String attributeClassName, String baseName, String label) {
079        this(RoleName.Builder.createWithClassName(attributeClassName, baseName, label));
080    }
081
082    @Override
083    public String getName() {
084        return this.name;
085    }
086
087    @Override
088    public String getBaseName() {
089        return this.baseName;
090    }
091
092    @Override
093    public String getReturnUrl() {
094        return this.returnUrl;
095    }
096
097    @Override
098    public String getLabel() {
099        return this.label;
100    }
101
102    public static String constructRoleValue(String attributeClassName, String roleName) {
103        return attributeClassName + "!" + roleName;
104    }
105
106
107    /**
108     * A builder which can be used to construct {@link RoleName} instances.  Enforces the constraints of the {@link RoleNameContract}.
109     *
110     */
111    public final static class Builder
112        implements Serializable, ModelBuilder, RoleNameContract
113    {
114
115        private String name;
116        private String baseName;
117        private String returnUrl;
118        private String label;
119
120        private Builder(String name, String baseName, String label) {
121            setName(name);
122            setBaseName(baseName);
123            setLabel(label);
124        }
125
126        public static Builder createWithClassName(String attributeClassName, String baseName, String label) {
127            return new Builder(constructRoleValue(attributeClassName, baseName), baseName, label);
128        }
129
130        public static Builder create(String name, String baseName, String label) {
131            return new Builder(name, baseName, label);
132        }
133
134        public static Builder create(RoleNameContract contract) {
135            if (contract == null) {
136                throw new IllegalArgumentException("contract was null");
137            }
138            Builder builder = create(contract.getName(), contract.getBaseName(), contract.getLabel());
139            builder.setReturnUrl(contract.getReturnUrl());
140            return builder;
141        }
142
143        public RoleName build() {
144            return new RoleName(this);
145        }
146
147        @Override
148        public String getName() {
149            return this.name;
150        }
151
152        @Override
153        public String getBaseName() {
154            return this.baseName;
155        }
156
157        @Override
158        public String getReturnUrl() {
159            return this.returnUrl;
160        }
161
162        @Override
163        public String getLabel() {
164            return this.label;
165        }
166
167        public void setName(String name) {
168            if (StringUtils.isBlank(name)) {
169                throw new RiceIllegalArgumentException("name is blank");
170            }
171            this.name = name;
172        }
173
174        public void setBaseName(String baseName) {
175            if (StringUtils.isBlank(baseName)) {
176                throw new RiceIllegalArgumentException("baseName is blank");
177            }
178            this.baseName = baseName;
179        }
180
181        public void setReturnUrl(String returnUrl) {
182            this.returnUrl = returnUrl;
183        }
184
185        public void setLabel(String label) {
186            if (StringUtils.isBlank(label)) {
187                throw new RiceIllegalArgumentException("label is blank");
188            }
189            this.label = label;
190        }
191
192    }
193
194
195    /**
196     * Defines some internal constants used on this class.
197     *
198     */
199    static class Constants {
200
201        final static String ROOT_ELEMENT_NAME = "roleName";
202        final static String TYPE_NAME = "RoleNameType";
203
204    }
205
206
207    /**
208     * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
209     *
210     */
211    static class Elements {
212
213        final static String NAME = "name";
214        final static String BASE_NAME = "baseName";
215        final static String RETURN_URL = "returnUrl";
216        final static String LABEL = "label";
217
218    }
219
220}