001/**
002 * Copyright 2005-2015 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kim.impl.role;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import javax.persistence.CascadeType;
022import javax.persistence.Column;
023import javax.persistence.Convert;
024import javax.persistence.Entity;
025import javax.persistence.GeneratedValue;
026import javax.persistence.Id;
027import javax.persistence.JoinColumn;
028import javax.persistence.OneToMany;
029import javax.persistence.Table;
030import javax.persistence.Transient;
031
032import org.eclipse.persistence.annotations.JoinFetch;
033import org.eclipse.persistence.annotations.JoinFetchType;
034import org.joda.time.DateTime;
035import org.kuali.rice.core.api.membership.MemberType;
036import org.kuali.rice.kim.api.role.Role;
037import org.kuali.rice.kim.api.services.KimApiServiceLocator;
038import org.kuali.rice.kim.api.type.KimType;
039import org.kuali.rice.kim.api.type.KimTypeInfoService;
040import org.kuali.rice.kim.framework.role.RoleEbo;
041import org.kuali.rice.kim.impl.type.KimTypeBo;
042import org.kuali.rice.krad.bo.DataObjectBase;
043import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
044import org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter;
045import org.springframework.util.AutoPopulatingList;
046
047@Entity
048@Table(name = "KRIM_ROLE_T")
049public class RoleBo extends DataObjectBase implements RoleEbo {
050
051    private static final long serialVersionUID = 1L;
052
053    @PortableSequenceGenerator(name = "KRIM_ROLE_ID_S")
054    @GeneratedValue(generator = "KRIM_ROLE_ID_S")
055    @Id
056    @Column(name = "ROLE_ID")
057    private String id;
058
059    @Column(name = "ROLE_NM")
060    private String name;
061
062    @Column(name = "DESC_TXT")
063    private String description;
064
065    @Column(name = "ACTV_IND")
066    @Convert(converter = BooleanYNConverter.class)
067    private boolean active;
068
069    @Column(name = "KIM_TYP_ID")
070    private String kimTypeId;
071
072    @Column(name = "NMSPC_CD")
073    private String namespaceCode;
074
075    @JoinFetch(value= JoinFetchType.OUTER)
076    @OneToMany(targetEntity = RoleMemberBo.class, orphanRemoval = true, cascade = CascadeType.ALL)
077    @JoinColumn(name = "ROLE_ID", referencedColumnName = "ROLE_ID", insertable = false, updatable = false)
078    private List<RoleMemberBo> members = new AutoPopulatingList<RoleMemberBo>(RoleMemberBo.class);
079
080    @Transient
081    private String principalName;
082
083    @Transient
084    private String groupNamespaceCode;
085
086    @Transient
087    private String groupName;
088
089    @Transient
090    private String permNamespaceCode;
091
092    @Transient
093    private String permName;
094
095    @Transient
096    private String permTmplNamespaceCode;
097
098    @Transient
099    private String permTmplName;
100
101    @Transient
102    private String respNamespaceCode;
103
104    @Transient
105    private String respName;
106
107    @Transient
108    private String respTmplNamespaceCode;
109
110    @Transient
111    private String respTmplName;
112
113    @Transient
114    private transient KimTypeInfoService kimTypeInfoService;
115
116    protected List<String> getMembersOfType(String memberTypeCode) {
117        List<String> roleMembers = new ArrayList<String>();
118        for (RoleMemberBo member : getMembers()) {
119            if (member.getType().equals(MemberType.valueOf(memberTypeCode)) && member.isActive(new DateTime())) {
120                roleMembers.add(member.getMemberId());
121            }
122        }
123        return roleMembers;
124    }
125
126    public KimTypeBo getKimRoleType() {
127        if (kimTypeId == null) {
128            return null;
129        }
130        KimType type = getTypeInfoService().getKimType(kimTypeId);
131        if (type == null) {
132            return null;
133        }
134        return KimTypeBo.from(type);
135    }
136
137    protected KimTypeInfoService getTypeInfoService() {
138        if (kimTypeInfoService == null) {
139            kimTypeInfoService = KimApiServiceLocator.getKimTypeInfoService();
140        }
141        return kimTypeInfoService;
142    }
143
144    public static Role to(RoleBo bo) {
145        if (bo == null) {
146            return null;
147        }
148        return Role.Builder.create(bo).build();
149    }
150
151    public static RoleBo from(Role immutable) {
152        if (immutable == null) {
153            return null;
154        }
155        RoleBo bo = new RoleBo();
156        bo.id = immutable.getId();
157        bo.name = immutable.getName();
158        bo.namespaceCode = immutable.getNamespaceCode();
159        bo.description = immutable.getDescription();
160        bo.kimTypeId = immutable.getKimTypeId();
161        bo.active = immutable.isActive();
162        bo.setVersionNumber(immutable.getVersionNumber());
163        bo.setObjectId(immutable.getObjectId());
164        return bo;
165    }
166
167    @Override
168    public String getId() {
169        return id;
170    }
171
172    public void setId(String id) {
173        this.id = id;
174    }
175
176    @Override
177    public String getName() {
178        return name;
179    }
180
181    public void setName(String name) {
182        this.name = name;
183    }
184
185    @Override
186    public String getDescription() {
187        return description;
188    }
189
190    public void setDescription(String description) {
191        this.description = description;
192    }
193
194    public boolean getActive() {
195        return active;
196    }
197
198    @Override
199    public boolean isActive() {
200        return active;
201    }
202
203    public void setActive(boolean active) {
204        this.active = active;
205    }
206
207    @Override
208    public String getKimTypeId() {
209        return kimTypeId;
210    }
211
212    public void setKimTypeId(String kimTypeId) {
213        this.kimTypeId = kimTypeId;
214    }
215
216    @Override
217    public String getNamespaceCode() {
218        return namespaceCode;
219    }
220
221    public void setNamespaceCode(String namespaceCode) {
222        this.namespaceCode = namespaceCode;
223    }
224
225    public List<RoleMemberBo> getMembers() {
226        return members;
227    }
228
229    public void setMembers(List<RoleMemberBo> members) {
230        this.members = members;
231    }
232
233    public String getPrincipalName() {
234        return principalName;
235    }
236
237    public void setPrincipalName(String principalName) {
238        this.principalName = principalName;
239    }
240
241    public String getGroupNamespaceCode() {
242        return groupNamespaceCode;
243    }
244
245    public void setGroupNamespaceCode(String groupNamespaceCode) {
246        this.groupNamespaceCode = groupNamespaceCode;
247    }
248
249    public String getGroupName() {
250        return groupName;
251    }
252
253    public void setGroupName(String groupName) {
254        this.groupName = groupName;
255    }
256
257    public String getPermNamespaceCode() {
258        return permNamespaceCode;
259    }
260
261    public void setPermNamespaceCode(String permNamespaceCode) {
262        this.permNamespaceCode = permNamespaceCode;
263    }
264
265    public String getPermName() {
266        return permName;
267    }
268
269    public void setPermName(String permName) {
270        this.permName = permName;
271    }
272
273    public String getPermTmplNamespaceCode() {
274        return permTmplNamespaceCode;
275    }
276
277    public void setPermTmplNamespaceCode(String permTmplNamespaceCode) {
278        this.permTmplNamespaceCode = permTmplNamespaceCode;
279    }
280
281    public String getPermTmplName() {
282        return permTmplName;
283    }
284
285    public void setPermTmplName(String permTmplName) {
286        this.permTmplName = permTmplName;
287    }
288
289    public String getRespNamespaceCode() {
290        return respNamespaceCode;
291    }
292
293    public void setRespNamespaceCode(String respNamespaceCode) {
294        this.respNamespaceCode = respNamespaceCode;
295    }
296
297    public String getRespName() {
298        return respName;
299    }
300
301    public void setRespName(String respName) {
302        this.respName = respName;
303    }
304
305    public String getRespTmplNamespaceCode() {
306        return respTmplNamespaceCode;
307    }
308
309    public void setRespTmplNamespaceCode(String respTmplNamespaceCode) {
310        this.respTmplNamespaceCode = respTmplNamespaceCode;
311    }
312
313    public String getRespTmplName() {
314        return respTmplName;
315    }
316
317    public void setRespTmplName(String respTmplName) {
318        this.respTmplName = respTmplName;
319    }
320
321    @Override
322    public void refresh() {
323    }
324}