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.dao.impl;
017    
018    import java.util.List;
019    
020    import javax.persistence.EntityManager;
021    import javax.persistence.PersistenceContext;
022    
023    import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
024    import org.kuali.rice.core.framework.persistence.jpa.criteria.Criteria;
025    import org.kuali.rice.core.framework.persistence.jpa.criteria.QueryByCriteria;
026    import org.kuali.rice.edl.impl.extract.Dump;
027    import org.kuali.rice.edl.impl.extract.Fields;
028    import org.kuali.rice.edl.impl.extract.dao.ExtractDAO;
029    import org.kuali.rice.kew.notes.Note;
030    
031    public class ExtractDAOJpaImpl implements ExtractDAO {
032    
033        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ExtractDAOJpaImpl.class);
034    
035        @PersistenceContext(unitName = "kew-unit")
036        private EntityManager entityManager;
037    
038        public Dump getDumpByDocumentId(String docId) {
039            LOG.debug("finding Document Extract by documentId " + docId);
040            Criteria crit = new Criteria(Dump.class.getName());
041            crit.eq("docId", docId);
042            return (Dump) new QueryByCriteria(entityManager, crit).toQuery().getSingleResult();
043        }
044    
045        public List<Fields> getFieldsByDocumentId(String docId) {
046            LOG.debug("finding Extract Fileds by documentId " + docId);
047            Criteria crit = new Criteria(Fields.class.getName());
048            crit.eq("documentId", docId);
049            crit.orderBy("docId", true);
050    
051            return (List<Fields>) new QueryByCriteria(entityManager, crit).toQuery().getResultList();
052        }
053    
054        public void saveDump(Dump dump) {
055            LOG.debug("check for null values in Extract document");
056            checkNull(dump.getDocId(), "Document ID");
057            checkNull(dump.getDocCreationDate(), "Creation Date");
058            checkNull(dump.getDocCurrentNodeName(), "Current Node Name");
059            checkNull(dump.getDocModificationDate(), "Modification Date");
060            checkNull(dump.getDocRouteStatusCode(), "Route Status Code");
061            checkNull(dump.getDocInitiatorId(), "Initiator ID");
062            checkNull(dump.getDocTypeName(), "Doc Type Name");
063            LOG.debug("saving EDocLite document: routeHeader " + dump.getDocId());
064            if (dump.getDocId() == null) {
065                entityManager.persist(dump);
066            } else {
067                OrmUtils.merge(entityManager, dump);
068            }
069        }
070    
071        public void saveField(Fields field) {
072            LOG.debug("saving EDocLite Extract fields");
073            checkNull(field.getDocId(), "Document ID");
074            checkNull(field.getFieldValue(), "Field Value");
075            checkNull(field.getFiledName(), "Field Name");
076            LOG.debug("saving Fields: routeHeader " + field.getFieldId());
077    
078            if (field.getFieldId() == null) {
079                entityManager.persist(field);
080            } else {
081                OrmUtils.merge(entityManager, field);
082            }
083        }
084    
085        private void checkNull(Object value, String valueName) throws RuntimeException {
086            if (value == null) {
087                throw new RuntimeException("Null value for " + valueName);
088            }
089        }
090    
091        public void deleteDump(String documentId) {
092            LOG.debug("deleting record form Extract Dump table");
093            entityManager.remove(entityManager.find(Note.class, documentId));
094        }
095    
096        public EntityManager getEntityManager() {
097            return this.entityManager;
098        }
099    
100        public void setEntityManager(EntityManager entityManager) {
101            this.entityManager = entityManager;
102        }
103    }