001/**
002 * Copyright 2005-2016 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.type;
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;
030
031import org.apache.commons.collections.CollectionUtils;
032import org.eclipse.persistence.annotations.JoinFetch;
033import org.eclipse.persistence.annotations.JoinFetchType;
034import org.kuali.rice.kim.api.type.KimType;
035import org.kuali.rice.kim.api.type.KimTypeAttribute;
036import org.kuali.rice.kim.api.type.KimTypeContract;
037import org.kuali.rice.krad.bo.BusinessObject;
038import org.kuali.rice.krad.bo.DataObjectBase;
039import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
040import org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter;
041import org.springframework.util.AutoPopulatingList;
042
043@Entity
044@Table(name = "KRIM_TYP_T")
045public class KimTypeBo extends DataObjectBase implements KimTypeContract, BusinessObject {
046    private static final long serialVersionUID = 1L;
047
048    @PortableSequenceGenerator(name = "KRIM_TYP_ID_S")
049    @GeneratedValue(generator = "KRIM_TYP_ID_S")
050    @Id
051    @Column(name = "KIM_TYP_ID")
052    private String id;
053
054    @Column(name = "SRVC_NM")
055    private String serviceName;
056
057    @Column(name = "NMSPC_CD")
058    private String namespaceCode;
059
060    @Column(name = "NM")
061    private String name;
062
063    @JoinFetch(value= JoinFetchType.OUTER)
064    @OneToMany(targetEntity = KimTypeAttributeBo.class, orphanRemoval = true, cascade = { CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.PERSIST })
065    @JoinColumn(name = "KIM_TYP_ID", referencedColumnName = "KIM_TYP_ID", insertable = false, updatable = false)
066    private List<KimTypeAttributeBo> attributeDefinitions = new AutoPopulatingList<KimTypeAttributeBo>(KimTypeAttributeBo.class);
067
068    @Column(name = "ACTV_IND")
069    @Convert(converter = BooleanYNConverter.class)
070    private boolean active;
071
072    /**
073     * Converts a mutable bo to its immutable counterpart
074     *
075     * @param bo the mutable business object
076     * @return the immutable object
077     */
078    public static KimType to(KimTypeBo bo) {
079        if (bo == null) {
080            return null;
081        }
082        return KimType.Builder.create(bo).build();
083    }
084
085    /**
086     * Converts a immutable object to its mutable counterpart
087     *
088     * @param im immutable object
089     * @return the mutable bo
090     */
091    public static KimTypeBo from(KimType im) {
092        if (im == null) {
093            return null;
094        }
095        KimTypeBo bo = new KimTypeBo();
096        bo.setId(im.getId());
097        bo.setServiceName(im.getServiceName());
098        bo.setNamespaceCode(im.getNamespaceCode());
099        bo.setName(im.getName());
100        List<KimTypeAttributeBo> attributeBos = new ArrayList<KimTypeAttributeBo>();
101        if (CollectionUtils.isNotEmpty(im.getAttributeDefinitions())) {
102            for (KimTypeAttribute kimTypeAttribute : im.getAttributeDefinitions()) {
103                attributeBos.add(KimTypeAttributeBo.from(kimTypeAttribute));
104            }
105            bo.setAttributeDefinitions(attributeBos);
106        }
107        bo.setActive(im.isActive());
108        bo.setVersionNumber(im.getVersionNumber());
109        bo.setObjectId(im.getObjectId());
110        return bo;
111    }
112
113    @Override
114    public String getId() {
115        return id;
116    }
117
118    public void setId(String id) {
119        this.id = id;
120    }
121
122    @Override
123    public String getServiceName() {
124        return serviceName;
125    }
126
127    public void setServiceName(String serviceName) {
128        this.serviceName = serviceName;
129    }
130
131    @Override
132    public String getNamespaceCode() {
133        return namespaceCode;
134    }
135
136    public void setNamespaceCode(String namespaceCode) {
137        this.namespaceCode = namespaceCode;
138    }
139
140    @Override
141    public String getName() {
142        return name;
143    }
144
145    public void setName(String name) {
146        this.name = name;
147    }
148
149    @Override
150    public List<KimTypeAttributeBo> getAttributeDefinitions() {
151        return attributeDefinitions;
152    }
153
154    public void setAttributeDefinitions(List<KimTypeAttributeBo> attributeDefinitions) {
155        this.attributeDefinitions = attributeDefinitions;
156    }
157
158    public boolean getActive() {
159        return active;
160    }
161
162    @Override
163    public boolean isActive() {
164        return active;
165    }
166
167    public void setActive(boolean active) {
168        this.active = active;
169    }
170
171    @Override
172    public void refresh() {
173    }
174}