001    /**
002     * Copyright 2005-2013 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     */
016    package org.kuali.rice.edl.impl.extract;
017    
018    import org.hibernate.annotations.Fetch;
019    import org.hibernate.annotations.FetchMode;
020    import org.kuali.rice.core.api.util.RiceConstants;
021    import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
022    import org.kuali.rice.edl.framework.extract.DumpDTO;
023    import org.kuali.rice.edl.framework.extract.FieldDTO;
024    import org.kuali.rice.kew.api.KewApiConstants;
025    import org.kuali.rice.krad.service.KRADServiceLocator;
026    
027    import javax.persistence.CascadeType;
028    import javax.persistence.Column;
029    import javax.persistence.Entity;
030    import javax.persistence.FetchType;
031    import javax.persistence.Id;
032    import javax.persistence.OneToMany;
033    import javax.persistence.Table;
034    import javax.persistence.Version;
035    import java.sql.Timestamp;
036    import java.text.DateFormat;
037    import java.text.SimpleDateFormat;
038    import java.util.ArrayList;
039    import java.util.Calendar;
040    import java.util.Date;
041    import java.util.List;
042    
043    /**
044     *
045     *
046     *
047     * @author Kuali Rice Team (rice.collab@kuali.org)
048     *
049     */
050    @Entity
051    @Table(name="KREW_EDL_DMP_T")
052    //@Sequence(name="KREW_EDL_DMP_T", property="docId")
053    public class Dump {
054    
055            private static final long serialVersionUID = -6136544551121011531L;
056    
057        @Id
058            @Column(name="DOC_HDR_ID")
059            private String docId;
060            @Column(name="DOC_TYP_NM")
061            private String docTypeName;
062            @Column(name="DOC_HDR_STAT_CD")
063            private String docRouteStatusCode;
064            @Column(name="DOC_HDR_MDFN_DT")
065            private Timestamp docModificationDate;
066            @Column(name="DOC_HDR_CRTE_DT")
067            private Timestamp docCreationDate;
068            @Column(name="DOC_HDR_TTL")
069            private String docDescription;
070        @Column(name="DOC_HDR_INITR_PRNCPL_ID")
071            private String docInitiatorId;
072        @Column(name="CRNT_NODE_NM")
073            private String docCurrentNodeName;
074        @Version
075            @Column(name="VER_NBR")
076            private Integer lockVerNbr;
077    
078        @OneToMany(fetch=FetchType.EAGER,cascade={CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REMOVE},mappedBy="dump")
079        @Fetch(value = FetchMode.SELECT)
080        private List<Fields> fields = new ArrayList<Fields>();
081    
082        //@PrePersist
083        public void beforeInsert(){
084            OrmUtils.populateAutoIncValue(this, KRADServiceLocator.getEntityManagerFactory().createEntityManager());
085        }
086    
087    
088            public Timestamp getDocCreationDate() {
089                    return docCreationDate;
090            }
091            public void setDocCreationDate(final Timestamp docCreationDate) {
092                    this.docCreationDate = docCreationDate;
093            }
094            public String getDocCurrentNodeName() {
095                    return docCurrentNodeName;
096            }
097            public void setDocCurrentNodeName(final String docCurrentNodeName) {
098                    this.docCurrentNodeName = docCurrentNodeName;
099            }
100            public String getDocDescription() {
101                    return docDescription;
102            }
103            public void setDocDescription(final String docDescription) {
104                    this.docDescription = docDescription;
105            }
106            public String getDocId() {
107                    return docId;
108            }
109            public String getDocInitiatorId() {
110                    return docInitiatorId;
111            }
112            public void setDocInitiatorId(final String docInitiatorId) {
113                    this.docInitiatorId = docInitiatorId;
114            }
115            public Timestamp getDocModificationDate() {
116                    return docModificationDate;
117            }
118            public void setDocModificationDate(final Timestamp docModificationDate) {
119                    this.docModificationDate = docModificationDate;
120            }
121            public String getDocRouteStatusCode() {
122                    return docRouteStatusCode;
123            }
124            public void setDocRouteStatusCode(final String docRouteStatusCode) {
125                    this.docRouteStatusCode = docRouteStatusCode;
126            }
127            public String getDocTypeName() {
128                    return docTypeName;
129            }
130            public void setDocTypeName(final String docTypeName) {
131                    this.docTypeName = docTypeName;
132            }
133            public Integer getLockVerNbr() {
134                    return lockVerNbr;
135            }
136            public void setLockVerNbr(final Integer lockVerNbr) {
137                    this.lockVerNbr = lockVerNbr;
138            }
139        public String getFormattedCreateDateTime() {
140            long time = getDocCreationDate().getTime();
141            Calendar calendar = Calendar.getInstance();
142            calendar.setTimeInMillis(time);
143            Date date = calendar.getTime();
144            DateFormat dateFormat = new SimpleDateFormat(KewApiConstants.TIMESTAMP_DATE_FORMAT_PATTERN2);
145            return dateFormat.format(date);
146        }
147    
148        public String getFormattedCreateDate() {
149            long time = getDocCreationDate().getTime();
150            Calendar calendar = Calendar.getInstance();
151            calendar.setTimeInMillis(time);
152            Date date = calendar.getTime();
153            DateFormat dateFormat = RiceConstants.getDefaultDateFormat();
154            return dateFormat.format(date);
155        }
156            public void setDocId(final String docId) {
157                    this.docId = docId;
158            }
159    
160    
161            public List<Fields> getFields() {
162                    return fields;
163            }
164    
165            public void setFields(final List<Fields> fields) {
166                    this.fields = fields;
167            }
168            
169            public static DumpDTO to(Dump dump) {
170                    if (dump == null) {
171                            return null;
172                    }
173                    DumpDTO dumpDTO = new DumpDTO();
174                    dumpDTO.setDocCreationDate(dump.getDocCreationDate());
175                    dumpDTO.setDocCurrentNodeName(dump.getDocCurrentNodeName());
176                    dumpDTO.setDocDescription(dump.getDocDescription());
177                    dumpDTO.setDocId(dump.getDocId());
178                    dumpDTO.setDocInitiatorId(dump.getDocInitiatorId());
179                    dumpDTO.setDocModificationDate(dump.getDocModificationDate());
180                    dumpDTO.setDocRouteStatusCode(dump.getDocRouteStatusCode());
181                    dumpDTO.setDocTypeName(dump.getDocTypeName());
182                    dumpDTO.setLockVerNbr(dump.getLockVerNbr());
183                    List<FieldDTO> fields = new ArrayList<FieldDTO>();
184                    for (Fields field : dump.getFields()) {
185                            fields.add(Fields.to(field));
186                    }
187                    dumpDTO.setFields(fields);
188                    return dumpDTO;
189            }
190            
191            public static Dump from(DumpDTO dumpDTO) {
192                    if (dumpDTO == null) {
193                            return null;
194                    }
195                    Dump dump = new Dump();
196                    dump.setDocCreationDate(dumpDTO.getDocCreationDate());
197                    dump.setDocCurrentNodeName(dumpDTO.getDocCurrentNodeName());
198                    dump.setDocDescription(dumpDTO.getDocDescription());
199                    dump.setDocId(dumpDTO.getDocId());
200                    dump.setDocInitiatorId(dumpDTO.getDocInitiatorId());
201                    dump.setDocModificationDate(dumpDTO.getDocModificationDate());
202                    dump.setDocRouteStatusCode(dumpDTO.getDocRouteStatusCode());
203                    dump.setDocTypeName(dumpDTO.getDocTypeName());
204                    dump.setLockVerNbr(dumpDTO.getLockVerNbr());
205                    List<Fields> fields = new ArrayList<Fields>();
206                    for (FieldDTO fieldDTO : dumpDTO.getFields()) {
207                            fields.add(Fields.from(fieldDTO, dump));
208                    }
209                    dump.setFields(fields);
210                    return dump;
211            }
212    
213    }
214