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.kew.api.rule;
017    
018    import java.io.Serializable;
019    import java.lang.reflect.Type;
020    import java.util.ArrayList;
021    import java.util.Collection;
022    import java.util.Collections;
023    import java.util.List;
024    import javax.xml.bind.annotation.XmlAccessType;
025    import javax.xml.bind.annotation.XmlAccessorType;
026    import javax.xml.bind.annotation.XmlAnyElement;
027    import javax.xml.bind.annotation.XmlElement;
028    import javax.xml.bind.annotation.XmlRootElement;
029    import javax.xml.bind.annotation.XmlType;
030    
031    import org.apache.commons.collections.CollectionUtils;
032    import org.apache.commons.lang.ObjectUtils;
033    import org.apache.commons.lang.StringUtils;
034    import org.apache.commons.lang.math.NumberUtils;
035    import org.kuali.rice.core.api.CoreConstants;
036    import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
037    import org.kuali.rice.core.api.mo.ModelBuilder;
038    import org.kuali.rice.kew.api.KewApiConstants;
039    import org.w3c.dom.Element;
040    
041    @XmlRootElement(name = RuleResponsibility.Constants.ROOT_ELEMENT_NAME)
042    @XmlAccessorType(XmlAccessType.NONE)
043    @XmlType(name = RuleResponsibility.Constants.TYPE_NAME, propOrder = {
044        RuleResponsibility.Elements.ID,
045        RuleResponsibility.Elements.PRIORITY,
046        RuleResponsibility.Elements.RESPONSIBILITY_ID,
047        RuleResponsibility.Elements.ACTION_REQUESTED_CD,
048        RuleResponsibility.Elements.APPROVE_POLICY,
049        RuleResponsibility.Elements.PRINCIPAL_ID,
050        RuleResponsibility.Elements.GROUP_ID,
051        RuleResponsibility.Elements.ROLE_NAME,
052        RuleResponsibility.Elements.DELEGATION_RULES,
053        RuleResponsibility.Elements.USING_ROLE,
054        RuleResponsibility.Elements.USING_GROUP,
055        RuleResponsibility.Elements.USING_PRINCIPAL,
056        CoreConstants.CommonElements.VERSION_NUMBER,
057        CoreConstants.CommonElements.OBJECT_ID,
058        CoreConstants.CommonElements.FUTURE_ELEMENTS
059    })
060    public final class RuleResponsibility
061        extends AbstractDataTransferObject
062        implements RuleResponsibilityContract
063    {
064        @XmlElement(name = Elements.ID, required = false)
065        private final String id;
066        @XmlElement(name = Elements.PRIORITY, required = false)
067        private final Integer priority;
068        @XmlElement(name = Elements.RESPONSIBILITY_ID, required = true)
069        private final String responsibilityId;
070        @XmlElement(name = Elements.ACTION_REQUESTED_CD, required = false)
071        private final String actionRequestedCd;
072        @XmlElement(name = Elements.APPROVE_POLICY, required = false)
073        private final String approvePolicy;
074        @XmlElement(name = Elements.PRINCIPAL_ID, required = false)
075        private final String principalId;
076        @XmlElement(name = Elements.GROUP_ID, required = false)
077        private final String groupId;
078        @XmlElement(name = Elements.ROLE_NAME, required = false)
079        private final String roleName;
080        @XmlElement(name = Elements.DELEGATION_RULES, required = false)
081        private final List<RuleDelegation> delegationRules;
082        @XmlElement(name = Elements.USING_ROLE, required = false)
083        private final boolean usingRole;
084        @XmlElement(name = Elements.USING_PRINCIPAL, required = false)
085        private final boolean usingPrincipal;
086        @XmlElement(name = Elements.USING_GROUP, required = false)
087        private final boolean usingGroup;
088        @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
089        private final Long versionNumber;
090        @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
091        private final String objectId;
092        @SuppressWarnings("unused")
093        @XmlAnyElement
094        private final Collection<Element> _futureElements = null;
095    
096        /**
097         * Private constructor used only by JAXB.
098         * 
099         */
100        private RuleResponsibility() {
101            this.id = null;
102            this.priority = null;
103            this.responsibilityId = null;
104            this.actionRequestedCd = null;
105            this.approvePolicy = null;
106            this.principalId = null;
107            this.groupId = null;
108            this.roleName = null;
109            this.delegationRules = null;
110            this.usingGroup = false;
111            this.usingPrincipal = false;
112            this.usingRole = false;
113            this.versionNumber = null;
114            this.objectId = null;
115        }
116    
117        private RuleResponsibility(Builder builder) {
118            this.id = builder.getId();
119            this.priority = builder.getPriority();
120            this.responsibilityId = builder.getResponsibilityId();
121            this.actionRequestedCd = builder.getActionRequestedCd();
122            this.approvePolicy = builder.getApprovePolicy();
123            this.principalId = builder.getPrincipalId();
124            this.groupId = builder.getGroupId();
125            this.roleName = builder.getRoleName();
126            if (CollectionUtils.isNotEmpty(builder.getDelegationRules())) {
127                List<RuleDelegation> delegationList = new ArrayList<RuleDelegation>();
128                for (RuleDelegation.Builder b : builder.getDelegationRules()) {
129                    delegationList.add(b.build());
130                }
131                this.delegationRules = delegationList;
132            } else {
133                this.delegationRules = Collections.emptyList();
134            }
135            this.usingGroup = builder.isUsingGroup();
136            this.usingPrincipal = builder.isUsingPrincipal();
137            this.usingRole = builder.isUsingRole();
138            versionNumber = builder.getVersionNumber();
139            objectId = builder.getObjectId();
140        }
141    
142        @Override
143        public String getId() {
144            return this.id;
145        }
146    
147        @Override
148        public Integer getPriority() {
149            return this.priority;
150        }
151    
152        @Override
153        public String getResponsibilityId() {
154            return this.responsibilityId;
155        }
156    
157        @Override
158        public String getActionRequestedCd() {
159            return this.actionRequestedCd;
160        }
161    
162        @Override
163        public String getApprovePolicy() {
164            return this.approvePolicy;
165        }
166    
167        @Override
168        public String getPrincipalId() {
169            return this.principalId;
170        }
171    
172        @Override
173        public String getGroupId() {
174            return this.groupId;
175        }
176    
177        @Override
178        public String getRoleName() {
179            return this.roleName;
180        }
181    
182        @Override
183        public List<RuleDelegation> getDelegationRules() {
184            return this.delegationRules;
185        }
186    
187        @Override
188        public boolean isUsingRole() {
189            return this.usingRole;
190        }
191    
192        @Override
193        public boolean isUsingPrincipal() {
194            return this.usingPrincipal;
195        }
196    
197        @Override
198        public boolean isUsingGroup() {
199            return this.usingGroup;
200        }
201    
202        @Override
203        public Long getVersionNumber() {
204            return versionNumber;
205        }
206    
207        @Override
208        public String getObjectId() {
209            return objectId;
210        }
211    
212        public String getRoleAttributeName() {
213                return getRoleName().substring(0, getRoleName().indexOf("!"));
214        }
215    
216        public String getResolvedRoleName() {
217            if (isUsingRole()) {
218                return getRoleName().substring(getRoleName().indexOf("!") + 1, getRoleName().length());
219            }
220            return null;
221        }
222    
223        /**
224         * A builder which can be used to construct {@link RuleResponsibility} instances.  Enforces the constraints of the {@link RuleResponsibilityContract}.
225         * 
226         */
227        public final static class Builder
228            implements Serializable, ModelBuilder, RuleResponsibilityContract
229        {
230            private String id;
231            private Integer priority;
232            private String responsibilityId;
233            private String actionRequestedCd;
234            private String approvePolicy;
235            private String principalId;
236            private String groupId;
237            private String roleName;
238            private List<RuleDelegation.Builder> delegationRules;
239            private boolean usingRole = false;
240            private boolean usingPrincipal = false;
241            private boolean usingGroup = false;
242            private Long versionNumber;
243            private String objectId;
244    
245            private Builder() {
246            }
247    
248            public static Builder create() {
249                return new Builder();
250            }
251    
252            public static Builder create(RuleResponsibilityContract contract) {
253                if (contract == null) {
254                    throw new IllegalArgumentException("contract was null");
255                }
256                Builder builder = create();
257                builder.setId(contract.getId());
258                builder.setPriority(contract.getPriority());
259                builder.setResponsibilityId(contract.getResponsibilityId());
260                builder.setActionRequestedCd(contract.getActionRequestedCd());
261                builder.setApprovePolicy(contract.getApprovePolicy());
262                builder.setPrincipalId(contract.getPrincipalId());
263                builder.setGroupId(contract.getGroupId());
264                builder.setRoleName(contract.getRoleName());
265                if (CollectionUtils.isNotEmpty(contract.getDelegationRules())) {
266                    List<RuleDelegation.Builder> builders = new ArrayList<RuleDelegation.Builder>();
267                    for (RuleDelegationContract delegationContract : contract.getDelegationRules()) {
268                        builders.add(RuleDelegation.Builder.create(delegationContract));
269                    }
270                    builder.setDelegationRules(builders);
271                } else {
272                    builder.setDelegationRules(Collections.<RuleDelegation.Builder>emptyList());
273                }
274                builder.setUsingGroup(contract.isUsingGroup());
275                builder.setUsingPrincipal(contract.isUsingPrincipal());
276                builder.setUsingRole(contract.isUsingRole());
277                builder.setVersionNumber(contract.getVersionNumber());
278                builder.setObjectId(contract.getObjectId());
279                return builder;
280            }
281    
282            public RuleResponsibility build() {
283                return new RuleResponsibility(this);
284            }
285    
286            @Override
287            public String getId() {
288                return this.id;
289            }
290    
291            @Override
292            public Integer getPriority() {
293                return this.priority;
294            }
295    
296            @Override
297            public String getResponsibilityId() {
298                return this.responsibilityId;
299            }
300    
301            @Override
302            public String getActionRequestedCd() {
303                return this.actionRequestedCd;
304            }
305    
306            @Override
307            public String getApprovePolicy() {
308                return this.approvePolicy;
309            }
310    
311            @Override
312            public String getPrincipalId() {
313                return this.principalId;
314            }
315    
316            @Override
317            public String getGroupId() {
318                return this.groupId;
319            }
320    
321            @Override
322            public String getRoleName() {
323                return this.roleName;
324            }
325    
326            @Override
327            public List<RuleDelegation.Builder> getDelegationRules() {
328                return this.delegationRules;
329            }
330    
331            @Override
332            public boolean isUsingRole() {
333                return this.usingRole;
334            }
335    
336            @Override
337            public boolean isUsingPrincipal() {
338                return this.usingPrincipal;
339            }
340    
341            @Override
342            public boolean isUsingGroup() {
343                return this.usingGroup;
344            }
345    
346            @Override
347            public Long getVersionNumber() {
348                return versionNumber;
349            }
350    
351            @Override
352            public String getObjectId() {
353                return objectId;
354            }
355    
356            public void setId(String id) {
357                if (StringUtils.isWhitespace(id)) {
358                    throw new IllegalArgumentException("id is whitespace");
359                }
360                this.id = id;
361            }
362            public void setPriority(Integer priority) {
363                this.priority = priority;
364            }
365    
366            public void setResponsibilityId(String responsibilityId) {
367                this.responsibilityId = responsibilityId;
368            }
369    
370            public void setActionRequestedCd(String actionRequestedCd) {
371                this.actionRequestedCd = actionRequestedCd;
372            }
373    
374            public void setApprovePolicy(String approvePolicy) {
375                this.approvePolicy = approvePolicy;
376            }
377    
378            public void setPrincipalId(String principalId) {
379                this.principalId = principalId;
380            }
381    
382            public void setGroupId(String groupId) {
383                this.groupId = groupId;
384            }
385    
386            public void setRoleName(String roleName) {
387                this.roleName = roleName;
388            }
389    
390            public void setDelegationRules(List<RuleDelegation.Builder> delegationRules) {
391                this.delegationRules = delegationRules;
392            }
393    
394            private void setUsingRole(boolean usingRole) {
395                this.usingRole = usingRole;
396            }
397    
398            private void setUsingPrincipal(boolean usingPrincipal) {
399                this.usingPrincipal = usingPrincipal;
400            }
401    
402            private void setUsingGroup(boolean usingGroup) {
403                this.usingGroup = usingGroup;
404            }
405    
406            public void setVersionNumber(Long versionNumber) {
407                this.versionNumber = versionNumber;
408            }
409    
410            public void setObjectId(String objectId) {
411                this.objectId = objectId;
412            }
413    
414    
415        }
416    
417        @Override
418        public boolean equals(Object o) {
419            if (o == null) { return false; }
420            if (!(o instanceof RuleResponsibilityContract)) { return false; }
421            RuleResponsibilityContract resp = (RuleResponsibilityContract) o;
422            return StringUtils.equals(getPrincipalId(),  resp.getPrincipalId()) &&
423                   StringUtils.equals(getRoleName(), resp.getRoleName()) &&
424                   StringUtils.equals(getGroupId(), resp.getGroupId()) &&
425                   StringUtils.equals(getActionRequestedCd(), resp.getActionRequestedCd()) &&
426                   ObjectUtils.equals(getPriority(), resp.getPriority()) &&
427                   StringUtils.equals(getApprovePolicy(), resp.getApprovePolicy());
428        }
429    
430        /**
431         * Defines some internal constants used on this class.
432         * 
433         */
434        static class Constants {
435    
436            final static String ROOT_ELEMENT_NAME = "ruleResponsibility";
437            final static String TYPE_NAME = "RuleResponsibilityType";
438    
439        }
440    
441    
442        /**
443         * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
444         * 
445         */
446        static class Elements {
447            final static String ID = "id";
448            final static String PRIORITY = "priority";
449            final static String RESPONSIBILITY_ID = "responsibilityId";
450            final static String ACTION_REQUESTED_CD = "actionRequestedCd";
451            final static String APPROVE_POLICY = "approvePolicy";
452            final static String PRINCIPAL_ID = "principalId";
453            final static String GROUP_ID = "groupId";
454            final static String ROLE_NAME = "roleName";
455            final static String DELEGATION_RULES = "delegationRules";
456            final static String USING_ROLE = "usingRole";
457            final static String USING_PRINCIPAL = "usingPrincipal";
458            final static String USING_GROUP = "usingGroup";
459        }
460    
461        public static class Cache {
462            public static final String NAME = KewApiConstants.Namespaces.KEW_NAMESPACE_2_0 + "/" + RuleResponsibility.Constants.TYPE_NAME;
463        }
464    }