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             if (StringUtils.isBlank(principalId)) {
157                 throw new IllegalArgumentException("principalId is blank");
158             }
159             this.principalId = principalId;
160         }
161 
162         @Override
163         public String getGroupId() {
164             return groupId;
165         }
166 
167         public void setGroupId(final String groupId) {
168         	if (StringUtils.isBlank(groupId)) {
169                 throw new IllegalArgumentException("groupId is blank");
170             }
171         	this.groupId = groupId;
172         }
173 
174 		@Override
175 		public List<DelegateType.Builder> getDelegates() {
176 			return delegates;
177 		}
178 		
179         public void setDelegates(final List<DelegateType.Builder> delegates) {
180         	if (delegates == null || delegates.isEmpty()) {
181                 throw new IllegalArgumentException("delegates is null or empty");
182             }
183         	this.delegates = Collections.unmodifiableList(new ArrayList<DelegateType.Builder>(delegates));
184         }		
185 		
186 		@Override
187 		public Assignee build() {
188 			return new Assignee(this);
189 		}
190        
191     }
192 
193     /**
194      * Defines some internal constants used on this class.
195      */
196     static class Constants {
197         final static String ROOT_ELEMENT_NAME = "assignee";
198         final static String TYPE_NAME = "assigneeType";
199     }
200 
201     /**
202      * A private class which exposes constants which define the XML element names to use
203      * when this object is marshalled to XML.
204      */
205     static class Elements {
206         final static String PRINCIPAL_ID = "principalId";
207         final static String GROUP_ID = "groupId";
208         final static String DELEGATES = "delegates";
209         final static String DELEGATE = "delegate";
210     }
211 }