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.department;
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.KPMENamespace;
28  import org.kuali.kpme.core.api.role.department.DepartmentPrincipalRoleMemberBoContract;
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 DepartmentPrincipalRoleMemberBo extends PrincipalRoleMemberBo implements DepartmentPrincipalRoleMemberBoContract {
40  
41  	private static final long serialVersionUID = 6571029461890035997L;
42  	private static final Logger LOG = Logger.getLogger(DepartmentPrincipalRoleMemberBo.class);
43  
44  	public static DepartmentPrincipalRoleMemberBo from(RoleMemberBo roleMemberBo, Map<String, String> attributes) {
45          if (roleMemberBo == null) { 
46          	return null;
47          }
48  
49          DepartmentPrincipalRoleMemberBo.Builder b = new DepartmentPrincipalRoleMemberBo.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 DepartmentPrincipalRoleMemberBo() {
62  		super();
63  	}
64  	
65      private DepartmentPrincipalRoleMemberBo(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  		if (roleId == null) {
87  			roleId = KimApiServiceLocator.getRoleService().getRoleIdByNamespaceCodeAndName(KPMENamespace.KPME_HR.getNamespaceCode(), roleName);
88  		}
89  		setRoleId(roleId);
90  	}
91  	
92  	public static final class Builder implements ModelBuilder, ModelObjectComplete {
93  
94  		private static final long serialVersionUID = 1635448868805664016L;
95  		
96  		private String id;
97          private String roleId;
98          private Map<String, String> attributes;
99          private String memberId;
100         private MemberType type;
101         private DateTime activeFromDate;
102         private DateTime activeToDate;
103         private Long versionNumber;
104         private String objectId;
105 
106         private Builder(String roleId, String memberId, MemberType type) {
107             setRoleId(roleId);
108             setMemberId(memberId);
109             setType(type);
110         }
111 
112         public static Builder create(String roleId, String id, String memberId,
113                                      MemberType memberType, DateTime activeFromDate, DateTime activeToDate, Map<String, String> attributes,String memberName,String memberNamespaceCode) {
114             Builder b = new Builder(roleId, memberId, memberType);
115             b.setId(id);
116             b.setActiveFromDate(activeFromDate);
117             b.setActiveToDate(activeToDate);
118             b.setAttributes(attributes);
119             return b;
120         }
121 
122         public static Builder create(RoleMemberContract contract) {
123             Builder b = new Builder(contract.getRoleId(), contract.getMemberId(), contract.getType());
124             b.setId(contract.getId());
125             b.setAttributes(contract.getAttributes());
126             b.setActiveFromDate(contract.getActiveFromDate());
127             b.setActiveToDate(contract.getActiveToDate());
128             b.setVersionNumber(contract.getVersionNumber());
129             b.setObjectId(contract.getObjectId());
130             return b;
131         }
132 
133         @Override
134         public DepartmentPrincipalRoleMemberBo build() {
135             return new DepartmentPrincipalRoleMemberBo(this);
136         }
137 
138         public String getId() {
139             return id;
140         }
141 
142         public void setId(String id) {
143             this.id = id;
144         }
145 
146         public String getRoleId() {
147             return roleId;
148         }
149 
150         public void setRoleId(String roleId) {
151             if (StringUtils.isEmpty(roleId)) {
152 //                throw new IllegalArgumentException("roleId is empty");
153             	LOG.warn("roleId is empty");
154             }
155             this.roleId = roleId;
156         }
157 
158         public Map<String, String> getAttributes() {
159             return attributes;
160         }
161 
162         public void setAttributes(Map<String, String> attributes) {
163             this.attributes = attributes;
164         }
165 
166         public String getMemberId() {
167             return memberId;
168         }
169 
170         public void setMemberId(String memberId) {
171             if (StringUtils.isBlank(memberId)) {
172 //                throw new IllegalArgumentException("memberId may not be null");
173             	LOG.warn("memberId may not be null");
174             }
175             this.memberId = memberId;
176         }
177 
178         public MemberType getType() {
179             return type;
180         }
181 
182         public void setType(final MemberType type) {
183             if (type == null) {
184             	LOG.warn("type is null");
185 //                throw new IllegalArgumentException("type is null");
186             }
187             this.type = type;
188         }
189 
190         public DateTime getActiveFromDate() {
191             return activeFromDate;
192         }
193 
194         public void setActiveFromDate(DateTime activeFromDate) {
195             this.activeFromDate = activeFromDate;
196         }
197 
198         public DateTime getActiveToDate() {
199             return activeToDate;
200         }
201 
202         public void setActiveToDate(DateTime activeToDate) {
203             this.activeToDate = activeToDate;
204         }
205 
206         public boolean isActive(DateTime activeAsOfDate) {
207             return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, activeAsOfDate);
208         }
209 
210         public boolean isActive() {
211             return InactivatableFromToUtils.isActive(activeFromDate, activeToDate, null);
212         }
213 
214         public Long getVersionNumber() {
215             return versionNumber;
216         }
217 
218         public void setVersionNumber(final Long versionNumber) {
219             this.versionNumber = versionNumber;
220         }
221 
222         public String getObjectId() {
223             return objectId;
224         }
225 
226         public void setObjectId(final String objectId) {
227             this.objectId = objectId;
228         }
229 
230         @Override
231         public int hashCode() {
232             return HashCodeBuilder.reflectionHashCode(this);
233         }
234 
235         @Override
236         public boolean equals(Object obj) {
237             return EqualsBuilder.reflectionEquals(obj, this);
238         }
239 
240         @Override
241         public String toString() {
242             return ToStringBuilder.reflectionToString(this);
243         }
244     }
245 
246 }