001/**
002 * Copyright 2005-2017 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.krms.impl.repository;
017
018import org.kuali.rice.core.api.mo.common.active.MutableInactivatable;
019import org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter;
020import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
021import org.kuali.rice.krms.api.repository.type.KrmsTypeAttribute;
022import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
023import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinitionContract;
024
025import javax.persistence.CascadeType;
026import javax.persistence.Column;
027import javax.persistence.Convert;
028import javax.persistence.Entity;
029import javax.persistence.FetchType;
030import javax.persistence.GeneratedValue;
031import javax.persistence.Id;
032import javax.persistence.JoinColumn;
033import javax.persistence.OneToMany;
034import javax.persistence.Table;
035import javax.persistence.Version;
036import java.io.Serializable;
037import java.util.ArrayList;
038import java.util.List;
039
040@Entity
041@Table(name = "KRMS_TYP_T")
042public class KrmsTypeBo implements MutableInactivatable, KrmsTypeDefinitionContract, Serializable {
043
044    private static final long serialVersionUID = 1l;
045
046    @PortableSequenceGenerator(name = "KRMS_TYP_S")
047    @GeneratedValue(generator = "KRMS_TYP_S")
048    @Id
049    @Column(name = "TYP_ID")
050    private String id;
051
052    @Column(name = "NM")
053    private String name;
054
055    @Column(name = "NMSPC_CD")
056    private String namespace;
057
058    @Column(name = "SRVC_NM")
059    private String serviceName;
060
061    @Column(name = "ACTV")
062    @Convert(converter = BooleanYNConverter.class)
063    private boolean active = true;
064
065    @Version
066    @Column(name="VER_NBR", length=8)
067    protected Long versionNumber;
068
069    @OneToMany(targetEntity = KrmsTypeAttributeBo.class, fetch = FetchType.LAZY, orphanRemoval = true,
070//            , mappedBy = "type",
071            cascade = { CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.PERSIST })
072    @JoinColumn(name = "TYP_ID", referencedColumnName = "TYP_ID")
073    private List<KrmsTypeAttributeBo> attributes;
074
075    /**
076     * Converts a mutable bo to it's immutable counterpart
077     *
078     * @param bo the mutable business object
079     * @return the immutable object
080     */
081    public static KrmsTypeDefinition to(KrmsTypeBo bo) {
082        if (bo == null) {
083            return null;
084        }
085
086        return KrmsTypeDefinition.Builder.create(bo).build();
087    }
088
089    /**
090     * Converts a immutable object to it's mutable bo counterpart
091     *
092     * @param im immutable object
093     * @return the mutable bo
094     */
095    public static KrmsTypeBo from(KrmsTypeDefinition im) {
096        if (im == null) {
097            return null;
098        }
099
100        KrmsTypeBo bo = new KrmsTypeBo();
101        bo.id = im.getId();
102        bo.name = im.getName();
103        bo.namespace = im.getNamespace();
104        bo.serviceName = im.getServiceName();
105        bo.active = im.isActive();
106        bo.attributes = new ArrayList<KrmsTypeAttributeBo>();
107
108        for (KrmsTypeAttribute attr : im.getAttributes()) {
109            KrmsTypeAttributeBo attrBo = KrmsTypeAttributeBo.from(attr);
110            bo.attributes.add(attrBo);
111            attrBo.setType(bo);
112        }
113
114        bo.setVersionNumber(im.getVersionNumber());
115
116        return bo;
117    }
118
119    public String getId() {
120        return id;
121    }
122
123    public void setId(String id) {
124        this.id = id;
125    }
126
127    public String getName() {
128        return name;
129    }
130
131    public void setName(String name) {
132        this.name = name;
133    }
134
135    public String getNamespace() {
136        return namespace;
137    }
138
139    public void setNamespace(String namespace) {
140        this.namespace = namespace;
141    }
142
143    public String getServiceName() {
144        return serviceName;
145    }
146
147    public void setServiceName(String serviceName) {
148        this.serviceName = serviceName;
149    }
150
151    public boolean getActive() {
152        return active;
153    }
154
155    public boolean isActive() {
156        return active;
157    }
158
159    public void setActive(boolean active) {
160        this.active = active;
161    }
162
163    public Long getVersionNumber() {
164        return versionNumber;
165    }
166
167    public void setVersionNumber(Long versionNumber) {
168        this.versionNumber = versionNumber;
169    }
170
171    public List<KrmsTypeAttributeBo> getAttributes() {
172        return attributes;
173    }
174
175    public void setAttributes(List<KrmsTypeAttributeBo> attributes) {
176        this.attributes = attributes;
177    }
178
179}