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.edl.impl.extract.dao.impl;
017
018import java.util.List;
019
020import org.kuali.rice.core.api.criteria.QueryByCriteria;
021import org.kuali.rice.edl.impl.extract.Dump;
022import org.kuali.rice.edl.impl.extract.Fields;
023import org.kuali.rice.edl.impl.extract.dao.ExtractDAO;
024import org.kuali.rice.kew.notes.Note;
025import org.kuali.rice.krad.data.DataObjectService;
026import org.kuali.rice.krad.data.PersistenceOption;
027
028import static org.kuali.rice.core.api.criteria.PredicateFactory.*;
029
030public class ExtractDAOJpaImpl implements ExtractDAO {
031
032    /** Logger for this class. */
033    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ExtractDAOJpaImpl.class);
034
035    /** Service that persists data to and from the underlying datasource. */
036    private DataObjectService dataObjectService;
037
038    /**
039     * {@inheritDoc}
040     */
041    @Override
042    public Dump getDumpByDocumentId(String docId) {
043        LOG.debug("finding Document Extract by documentId " + docId);
044        return this.dataObjectService.find(Dump.class, docId);
045    }
046
047    /**
048     * {@inheritDoc}
049     */
050    @Override
051    public List<Fields> getFieldsByDocumentId(String docId) {
052        LOG.debug("finding Extract Fileds by documentId " + docId);
053
054        QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
055        criteria.setPredicates(equal("documentId", docId)).setOrderByAscending("docId");
056
057        return this.dataObjectService.findMatching(Fields.class, criteria.build()).getResults();
058    }
059
060    /**
061     * {@inheritDoc}
062     */
063    @Override
064    public Dump saveDump(Dump dump) {
065        LOG.debug("check for null values in Extract document");
066        checkNull(dump.getDocId(), "Document ID");
067        checkNull(dump.getDocCreationDate(), "Creation Date");
068        checkNull(dump.getDocCurrentNodeName(), "Current Node Name");
069        checkNull(dump.getDocModificationDate(), "Modification Date");
070        checkNull(dump.getDocRouteStatusCode(), "Route Status Code");
071        checkNull(dump.getDocInitiatorId(), "Initiator ID");
072        checkNull(dump.getDocTypeName(), "Doc Type Name");
073        LOG.debug("saving EDocLite document: routeHeader " + dump.getDocId());
074
075        return this.dataObjectService.save(dump, PersistenceOption.FLUSH);
076    }
077
078    /**
079     * {@inheritDoc}
080     */
081    @Override
082    public Fields saveField(Fields field) {
083        LOG.debug("saving EDocLite Extract fields");
084        checkNull(field.getDocId(), "Document ID");
085        checkNull(field.getFieldValue(), "Field Value");
086        checkNull(field.getFieldName(), "Field Name");
087        LOG.debug("saving Fields: routeHeader " + field.getFieldId());
088
089        return this.dataObjectService.save(field, PersistenceOption.FLUSH);
090    }
091
092    /**
093     * Determines if the given value is null and throws a {@link RuntimeException}
094     * @param value the value to check if null
095     * @param valueName the value name to display in the {@link RuntimeException} message.
096     * @throws RuntimeException if the supplied value is null
097     */
098    private void checkNull(Object value, String valueName) throws RuntimeException {
099        if (value == null) {
100            throw new RuntimeException("Null value for " + valueName);
101        }
102    }
103
104    /**
105     * {@inheritDoc}
106     */
107    @Override
108    public void deleteDump(String documentId) {
109        LOG.debug("deleting record form Extract Dump table");
110        this.dataObjectService.delete(this.dataObjectService.find(Note.class, documentId));
111    }
112
113    /**
114     * Returns the {@link DataObjectService}
115     * @return the {@link DataObjectService}
116     */
117    public DataObjectService getDataObjectService() {
118        return this.dataObjectService;
119    }
120
121    /**
122     *
123     * @see #getDataObjectService()
124     */
125    public void setDataObjectService(DataObjectService dataObjectService) {
126        this.dataObjectService = dataObjectService;
127    }
128}