001/**
002 * Copyright 2005-2016 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.peopleflow;
017
018import org.apache.commons.collections.CollectionUtils;
019import org.apache.commons.lang.StringUtils;
020import org.kuali.rice.core.api.CoreConstants;
021import org.kuali.rice.core.api.membership.MemberType;
022import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
023import org.kuali.rice.core.api.mo.ModelBuilder;
024import org.kuali.rice.core.api.mo.ModelObjectUtils;
025import org.kuali.rice.kew.api.action.ActionRequestPolicy;
026import org.w3c.dom.Element;
027
028import javax.xml.bind.annotation.XmlAccessType;
029import javax.xml.bind.annotation.XmlAccessorType;
030import javax.xml.bind.annotation.XmlAnyElement;
031import javax.xml.bind.annotation.XmlElement;
032import javax.xml.bind.annotation.XmlElementWrapper;
033import javax.xml.bind.annotation.XmlRootElement;
034import javax.xml.bind.annotation.XmlType;
035import java.io.Serializable;
036import java.util.ArrayList;
037import java.util.Collection;
038import java.util.List;
039
040@XmlRootElement(name = PeopleFlowMember.Constants.ROOT_ELEMENT_NAME)
041@XmlAccessorType(XmlAccessType.NONE)
042@XmlType(name = PeopleFlowMember.Constants.TYPE_NAME, propOrder = {
043        PeopleFlowMember.Elements.MEMBER_ID,
044        PeopleFlowMember.Elements.MEMBER_TYPE,
045        PeopleFlowMember.Elements.ACTION_REQUEST_POLICY,
046        PeopleFlowMember.Elements.RESPONSIBILITY_ID,
047        PeopleFlowMember.Elements.PRIORITY,
048        PeopleFlowMember.Elements.DELEGATES,
049        CoreConstants.CommonElements.FUTURE_ELEMENTS
050})
051public final class PeopleFlowMember extends AbstractDataTransferObject implements PeopleFlowMemberContract {
052
053    private static final int STARTING_PRIORITY = 1;
054
055    @XmlElement(name = Elements.MEMBER_ID, required = true)
056    private final String memberId;
057
058    @XmlElement(name = Elements.MEMBER_TYPE, required = true)
059    private final MemberType memberType;
060
061    @XmlElement(name = Elements.ACTION_REQUEST_POLICY, required = false)
062    private final ActionRequestPolicy actionRequestPolicy;
063
064    @XmlElement(name = Elements.RESPONSIBILITY_ID, required = false)
065    private final String responsibilityId;
066
067    @XmlElement(name = Elements.PRIORITY, required = true)
068    private final int priority;
069
070    @XmlElementWrapper(name = Elements.DELEGATES, required = false)
071    @XmlElement(name = Elements.DELEGATE, required = false)
072    private final List<PeopleFlowDelegate> delegates;
073
074    @SuppressWarnings("unused")
075    @XmlAnyElement
076    private final Collection<Element> _futureElements = null;
077
078    /**
079     * Private constructor used only by JAXB.
080     */
081    private PeopleFlowMember() {
082        this.memberId = null;
083        this.memberType = null;
084        this.actionRequestPolicy = null;
085        this.responsibilityId = null;
086        this.priority = STARTING_PRIORITY;
087        this.delegates = null;
088    }
089
090    private PeopleFlowMember(Builder builder) {
091        this.memberId = builder.getMemberId();
092        this.memberType = builder.getMemberType();
093        this.actionRequestPolicy = builder.getActionRequestPolicy();
094        this.responsibilityId = builder.getResponsibilityId();
095        this.priority = builder.getPriority();
096        this.delegates = ModelObjectUtils.buildImmutableCopy(builder.getDelegates());
097    }
098
099    @Override
100    public String getMemberId() {
101        return this.memberId;
102    }
103
104    @Override
105    public MemberType getMemberType() {
106        return this.memberType;
107    }
108
109    @Override
110    public ActionRequestPolicy getActionRequestPolicy() {
111        return this.actionRequestPolicy;
112    }
113
114    @Override
115    public String getResponsibilityId() {
116        return this.responsibilityId;
117    }
118
119    @Override
120    public int getPriority() {
121        return this.priority;
122    }
123
124    @Override
125    public List<PeopleFlowDelegate> getDelegates() {
126        return this.delegates;
127    }
128
129    /**
130     * A builder which can be used to construct {@link PeopleFlowMember} instances.  Enforces the constraints of the
131     * {@link PeopleFlowMemberContract}.
132     */
133    public final static class Builder implements Serializable, ModelBuilder, PeopleFlowMemberContract {
134
135        private String memberId;
136        private MemberType memberType;
137        private ActionRequestPolicy actionRequestPolicy;
138        private String responsibilityId;
139        private int priority;
140        private List<PeopleFlowDelegate.Builder> delegates;
141
142        private Builder(String memberId, MemberType memberType) {
143            setMemberId(memberId);
144            setMemberType(memberType);
145            setPriority(STARTING_PRIORITY);
146            setDelegates(new ArrayList<PeopleFlowDelegate.Builder>());
147        }
148
149        public static Builder create(String memberId, MemberType memberType) {
150            return new Builder(memberId, memberType);
151        }
152
153        public static Builder create(PeopleFlowMemberContract contract) {
154            Builder builder = createCopy(contract);
155
156            builder.setResponsibilityId(contract.getResponsibilityId());
157            return builder;
158        }
159
160        public static Builder createCopy(PeopleFlowMemberContract contract) {
161            if (contract == null) {
162                throw new IllegalArgumentException("contract was null");
163            }
164            Builder builder = create(contract.getMemberId(), contract.getMemberType());
165            builder.setActionRequestPolicy(contract.getActionRequestPolicy());
166            builder.setPriority(contract.getPriority());
167            if (CollectionUtils.isNotEmpty(contract.getDelegates())) {
168                for (PeopleFlowDelegateContract delegate : contract.getDelegates()) {
169                    builder.getDelegates().add(PeopleFlowDelegate.Builder.create(delegate));
170                }
171            }
172            return builder;
173        }
174
175        public PeopleFlowMember build() {
176            return new PeopleFlowMember(this);
177        }
178
179        @Override
180        public String getMemberId() {
181            return this.memberId;
182        }
183
184        @Override
185        public MemberType getMemberType() {
186            return this.memberType;
187        }
188
189        @Override
190        public ActionRequestPolicy getActionRequestPolicy() {
191            return this.actionRequestPolicy;
192        }
193
194        @Override
195        public String getResponsibilityId() {
196            return this.responsibilityId;
197        }
198
199        @Override
200        public int getPriority() {
201            return this.priority;
202        }
203
204        @Override
205        public List<PeopleFlowDelegate.Builder> getDelegates() {
206            return delegates;
207        }
208
209        public void setMemberId(String memberId) {
210            if (StringUtils.isBlank(memberId)) {
211                throw new IllegalArgumentException("memberId was null or blank");
212            }
213            this.memberId = memberId;
214        }
215
216        public void setMemberType(MemberType memberType) {
217            if (memberType == null) {
218                throw new IllegalArgumentException("memberType was null");
219            }
220            this.memberType = memberType;
221        }
222
223        public void setActionRequestPolicy(ActionRequestPolicy actionRequestPolicy) {
224            if (this.memberType.equals(MemberType.ROLE)) {
225                if (actionRequestPolicy == null) {
226                    throw new IllegalArgumentException("actionRequestPolicy was null");
227                }
228                this.actionRequestPolicy = actionRequestPolicy;
229            }
230        }
231
232        public void setResponsibilityId(String responsibilityId) {
233            this.responsibilityId = responsibilityId;
234        }
235
236        public void setPriority(int priority) {
237            if (priority < STARTING_PRIORITY) {
238                throw new IllegalArgumentException("Given priority was smaller than the minimum prior value of " + STARTING_PRIORITY);
239            }
240            this.priority = priority;
241        }
242
243        public void setDelegates(List<PeopleFlowDelegate.Builder> delegates) {
244            this.delegates = delegates;
245        }
246    }
247
248    /**
249     * Defines some internal constants used on this class.
250     */
251    static class Constants {
252        final static String ROOT_ELEMENT_NAME = "peopleFlowMember";
253        final static String TYPE_NAME = "PeopleFlowMemberType";
254    }
255
256    /**
257     * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
258     */
259    static class Elements {
260        final static String MEMBER_ID = "memberId";
261        final static String MEMBER_TYPE = "memberType";
262        final static String ACTION_REQUEST_POLICY = "actionRequestPolicy";
263        final static String RESPONSIBILITY_ID = "responsibilityId";
264        final static String PRIORITY = "priority";
265        final static String DELEGATES = "delegates";
266        final static String DELEGATE = "delegate";
267    }
268
269}