View Javadoc

1   /*
2    * Copyright 2006-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.kuali.rice.kim.api.common.assignee;
18  
19  import org.apache.commons.collections.CollectionUtils;
20  import org.apache.commons.lang.StringUtils;
21  import org.kuali.rice.core.api.CoreConstants;
22  import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
23  import org.kuali.rice.core.api.mo.ModelBuilder;
24  import org.kuali.rice.kim.api.common.delegate.DelegateType;
25  import org.kuali.rice.kim.api.common.delegate.DelegateTypeContract;
26  import org.w3c.dom.Element;
27  
28  import javax.xml.bind.annotation.XmlAccessType;
29  import javax.xml.bind.annotation.XmlAccessorType;
30  import javax.xml.bind.annotation.XmlAnyElement;
31  import javax.xml.bind.annotation.XmlElement;
32  import javax.xml.bind.annotation.XmlElementWrapper;
33  import javax.xml.bind.annotation.XmlRootElement;
34  import javax.xml.bind.annotation.XmlType;
35  import java.io.Serializable;
36  import java.util.ArrayList;
37  import java.util.Collection;
38  import java.util.Collections;
39  import java.util.List;
40  
41  
42  @XmlRootElement(name = Assignee.Constants.ROOT_ELEMENT_NAME)
43  @XmlAccessorType(XmlAccessType.NONE)
44  @XmlType(name = Assignee.Constants.TYPE_NAME, propOrder = {
45          Assignee.Elements.PRINCIPAL_ID,
46          Assignee.Elements.GROUP_ID,
47          Assignee.Elements.DELEGATES,
48          CoreConstants.CommonElements.FUTURE_ELEMENTS
49  })
50  public class Assignee extends AbstractDataTransferObject implements AssigneeContract {
51      @XmlElement(name = Elements.PRINCIPAL_ID, required = false)
52      private final String principalId;
53  
54      @XmlElement(name = Elements.GROUP_ID, required = true)
55      private final String groupId;
56  
57      @XmlElementWrapper(name = Elements.DELEGATES, required = false)
58      @XmlElement(name = Elements.DELEGATE, required = false)
59      private final List<DelegateType> delegates;
60  
61      @SuppressWarnings("unused")
62      @XmlAnyElement
63      private final Collection<Element> _futureElements = null;
64  
65      /**
66  	 *  A constructor to be used only by JAXB unmarshalling.
67  	 *  
68  	 */
69      private Assignee() {
70          this.principalId = null;
71          this.groupId = null;
72          this.delegates = null;
73      }
74   
75      /**
76  	 * A constructor using the Builder.
77  	 * 
78  	 * @param builder
79  	 */
80      public Assignee(Builder builder) {
81          this.principalId = builder.getPrincipalId();
82          this.groupId = builder.getGroupId();
83          final List<DelegateType> temp = new ArrayList<DelegateType>();
84          if (!CollectionUtils.isEmpty(builder.getDelegates())) {
85              for (DelegateType.Builder delegate: builder.getDelegates()) {
86                  temp.add(delegate.build());
87              }
88          }
89          this.delegates = Collections.unmodifiableList(temp);
90      }
91  
92  	/**
93  	 * @see AssigneeContract#getPrincipalId()
94  	 */
95  	@Override
96  	public String getPrincipalId() {
97  		return this.principalId;
98  	}
99  
100 	/**
101 	 * @see AssigneeContract#getGroupId()
102 	 */
103 	@Override
104 	public String getGroupId() {
105 		return this.groupId;
106 	}
107 
108 	/**
109 	 * @see AssigneeContract#getDelegates()
110 	 */
111 	@Override
112 	public List<DelegateType> getDelegates() {
113 		return this.delegates;
114 	}
115 
116     /**
117      * This builder constructs a PermissionAssignee enforcing the constraints of the {@link AssigneeContract}.
118      */
119     public static final class Builder implements AssigneeContract, ModelBuilder, Serializable {
120         private String principalId;
121         private String groupId;
122         private List<DelegateType.Builder> delegates;
123 
124         private Builder(String principalId, String groupId, List<DelegateType.Builder> delegates) {
125             setPrincipalId(principalId);
126             setGroupId(groupId);
127             setDelegates(delegates);
128         }
129 
130         /**
131          * Creates a KimAttributeData with the required fields.
132          */
133         public static Builder create(String principalId, String groupId, List<DelegateType.Builder> delegates) {
134             return new Builder(principalId, groupId, delegates);
135         }
136 
137         /**
138          * creates a KimAttributeData from an existing {@link org.kuali.rice.kim.api.common.attribute.KimAttributeDataContract}.
139          */
140         public static Builder create(AssigneeContract contract) {
141             final List<DelegateType.Builder> builders = new ArrayList<DelegateType.Builder>();
142             for (DelegateTypeContract d : contract.getDelegates()) {
143                 builders.add(DelegateType.Builder.create(d));
144             }
145 
146             Builder builder = new Builder(contract.getPrincipalId(), contract.getGroupId(), builders);
147             return builder;
148         }
149 
150         @Override
151         public String getPrincipalId() {
152             return principalId;
153         }
154 
155         public void setPrincipalId(final String principalId) {
156             this.principalId = principalId;
157         }
158 
159         @Override
160         public String getGroupId() {
161             return groupId;
162         }
163 
164         public void setGroupId(final String groupId) {
165         	this.groupId = groupId;
166         }
167 
168 		@Override
169 		public List<DelegateType.Builder> getDelegates() {
170 			return delegates;
171 		}
172 		
173         public void setDelegates(final List<DelegateType.Builder> delegates) {
174         	this.delegates = Collections.unmodifiableList(new ArrayList<DelegateType.Builder>(delegates));
175         }		
176 		
177 		@Override
178 		public Assignee build() {
179 		    //validate required fields
180             final boolean requiredSet = (groupId != null ^ principalId != null) && delegates != null;
181             if (!requiredSet) {
182                 throw new IllegalStateException("all the required fields are not set");
183             }
184 
185 			return new Assignee(this);
186 		}
187        
188     }
189 
190     /**
191      * Defines some internal constants used on this class.
192      */
193     static class Constants {
194         final static String ROOT_ELEMENT_NAME = "assignee";
195         final static String TYPE_NAME = "assigneeType";
196     }
197 
198     /**
199      * A private class which exposes constants which define the XML element names to use
200      * when this object is marshalled to XML.
201      */
202     static class Elements {
203         final static String PRINCIPAL_ID = "principalId";
204         final static String GROUP_ID = "groupId";
205         final static String DELEGATES = "delegates";
206         final static String DELEGATE = "delegate";
207     }
208 }