View Javadoc
1   /**
2    * Copyright 2004-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.kpme.core.role.location;
17  
18  import java.sql.Timestamp;
19  import java.util.Map;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.apache.commons.lang.builder.EqualsBuilder;
23  import org.apache.commons.lang.builder.HashCodeBuilder;
24  import org.apache.commons.lang.builder.ToStringBuilder;
25  import org.apache.log4j.Logger;
26  import org.joda.time.DateTime;
27  import org.kuali.kpme.core.api.namespace.KPMENamespace;
28  import org.kuali.kpme.core.api.role.location.LocationPrincipalRoleMemberBoContract;
29  import org.kuali.kpme.core.role.PrincipalRoleMemberBo;
30  import org.kuali.rice.core.api.membership.MemberType;
31  import org.kuali.rice.core.api.mo.ModelBuilder;
32  import org.kuali.rice.core.api.mo.ModelObjectComplete;
33  import org.kuali.rice.core.api.mo.common.active.InactivatableFromToUtils;
34  import org.kuali.rice.kim.api.role.RoleMemberContract;
35  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
36  import org.kuali.rice.kim.impl.role.RoleMemberBo;
37  
38  @SuppressWarnings("unchecked")
39  public class LocationPrincipalRoleMemberBo extends PrincipalRoleMemberBo implements LocationPrincipalRoleMemberBoContract {
40  
41  	private static final long serialVersionUID = -7015816117671681162L;
42  	private static final Logger LOG = Logger.getLogger(LocationPrincipalRoleMemberBo.class);
43  
44  	public static LocationPrincipalRoleMemberBo from(RoleMemberBo roleMemberBo, Map<String, String> attributes) {
45          if (roleMemberBo == null) { 
46          	return null;
47          }
48  
49          LocationPrincipalRoleMemberBo.Builder b = new LocationPrincipalRoleMemberBo.Builder(roleMemberBo.getRoleId(), roleMemberBo.getMemberId(), roleMemberBo.getType());
50          b.setId(roleMemberBo.getId());
51          b.setActiveFromDate(roleMemberBo.getActiveFromDate());
52          b.setActiveToDate(roleMemberBo.getActiveToDate());
53          b.setObjectId(roleMemberBo.getObjectId());
54          b.setVersionNumber(roleMemberBo.getVersionNumber());
55          
56          b.setAttributes(attributes);
57          
58          return b.build();
59      }
60  	
61  	public LocationPrincipalRoleMemberBo() {
62  		super();
63  	}
64  	
65      private LocationPrincipalRoleMemberBo(Builder b) {
66          setId(b.getId());
67          setRoleId(b.getRoleId());
68          setAttributes(b.getAttributes());
69          setMemberId(b.getMemberId());
70          setTypeCode(b.getType().getCode());
71          setActiveFromDateValue(b.getActiveFromDate() == null ? null : new Timestamp(b.getActiveFromDate().getMillis()));
72          setActiveToDateValue(b.getActiveToDate() == null ? null : new Timestamp(b.getActiveToDate().getMillis()));
73          setVersionNumber(b.getVersionNumber());
74          setObjectId(b.getObjectId());
75      }
76      
77      @Override
78  	public void setRoleName(String roleName) {
79  
80      	String roleId = KimApiServiceLocator.getRoleService().getRoleIdByNamespaceCodeAndName(KPMENamespace.KPME_TK.getNamespaceCode(), roleName);
81  		
82  		if (roleId == null) {
83  			roleId = KimApiServiceLocator.getRoleService().getRoleIdByNamespaceCodeAndName(KPMENamespace.KPME_LM.getNamespaceCode(), roleName);
84  		}
85  		
86  		setRoleId(roleId);
87  	}
88  	
89  	public static final class Builder implements ModelBuilder, ModelObjectComplete {
90  
91  		private static final long serialVersionUID = 8683798354938317359L;
92  		
93  		private String id;
94          private String roleId;
95          private Map<String, String> attributes;
96          private String memberId;
97          private MemberType type;
98          private DateTime activeFromDate;
99          private DateTime activeToDate;
100         private Long versionNumber;
101         private String objectId;
102 
103         private Builder(String roleId, String memberId, MemberType type) {
104             setRoleId(roleId);
105             setMemberId(memberId);
106             setType(type);
107         }
108 
109         public static Builder create(String roleId, String id, String memberId,
110                                      MemberType memberType, DateTime activeFromDate, DateTime activeToDate, Map<String, String> attributes,String memberName,String memberNamespaceCode) {
111             Builder b = new Builder(roleId, memberId, memberType);
112             b.setId(id);
113             b.setActiveFromDate(activeFromDate);
114             b.setActiveToDate(activeToDate);
115             b.setAttributes(attributes);
116             return b;
117         }
118 
119         public static Builder create(RoleMemberContract contract) {
120             Builder b = new Builder(contract.getRoleId(), contract.getMemberId(), contract.getType());
121             b.setId(contract.getId());
122             b.setAttributes(contract.getAttributes());
123             b.setActiveFromDate(contract.getActiveFromDate());
124             b.setActiveToDate(contract.getActiveToDate());
125             b.setVersionNumber(contract.getVersionNumber());
126             b.setObjectId(contract.getObjectId());
127             return b;
128         }
129 
130         @Override
131         public LocationPrincipalRoleMemberBo build() {
132             return new LocationPrincipalRoleMemberBo(this);
133         }
134 
135         public String getId() {
136             return id;
137         }
138 
139         public void setId(String id) {
140             this.id = id;
141         }
142 
143         public String getRoleId() {
144             return roleId;
145         }
146 
147         public void setRoleId(String roleId) {
148             if (StringUtils.isEmpty(roleId)) {
149 //                throw new IllegalArgumentException("roleId is empty");
150             	LOG.warn("roleId is empty");
151             }
152             this.roleId = roleId;
153         }
154 
155         public Map<String, String> getAttributes() {
156             return attributes;
157         }
158 
159         public void setAttributes(Map<String, String> attributes) {
160             this.attributes = attributes;
161         }
162 
163         public String getMemberId() {
164             return memberId;
165         }
166 
167         public void setMemberId(String memberId) {
168             if (StringUtils.isBlank(memberId)) {
169             	LOG.warn("memberId may not be null");
170 //                throw new IllegalArgumentException("memberId may not be null");
171             }
172             this.memberId = memberId;
173         }
174 
175         public MemberType getType() {
176             return type;
177         }
178 
179         public void setType(final MemberType type) {
180             if (type == null) {
181             	LOG.warn("type is null");
182 //                throw new IllegalArgumentException("type is null");
183             }
184             this.type = type;
185         }
186 
187         public DateTime getActiveFromDate() {
188             return activeFromDate;
189         }
190 
191         public void setActiveFromDate(DateTime activeFromDate) {
192             this.activeFromDate = activeFromDate;
193         }
194 
195         public DateTime getActiveToDate() {
196             return activeToDate;
197         }
198 
199         public void setActiveToDate(DateTime activeToDate) {
200             this.activeToDate = activeToDate;
201         }
202 
203         public boolean isActive(DateTime activeAsOfDate) {
204             return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, activeAsOfDate);
205         }
206 
207         public boolean isActive() {
208             return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, null);
209         }
210 
211         public Long getVersionNumber() {
212             return versionNumber;
213         }
214 
215         public void setVersionNumber(final Long versionNumber) {
216             this.versionNumber = versionNumber;
217         }
218 
219         public String getObjectId() {
220             return objectId;
221         }
222 
223         public void setObjectId(final String objectId) {
224             this.objectId = objectId;
225         }
226 
227         @Override
228         public int hashCode() {
229             return HashCodeBuilder.reflectionHashCode(this);
230         }
231 
232         @Override
233         public boolean equals(Object obj) {
234             return EqualsBuilder.reflectionEquals(obj, this);
235         }
236 
237         @Override
238         public String toString() {
239             return ToStringBuilder.reflectionToString(this);
240         }
241     }
242 
243 }