View Javadoc

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