001/**
002 * Copyright 2005-2015 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.dao.impl;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.kuali.rice.core.api.criteria.Predicate;
022import org.kuali.rice.core.api.criteria.QueryByCriteria;
023import org.kuali.rice.edl.impl.bo.EDocLiteAssociation;
024import org.kuali.rice.edl.impl.bo.EDocLiteDefinition;
025import org.kuali.rice.edl.impl.dao.EDocLiteDAO;
026import org.kuali.rice.krad.data.DataObjectService;
027import org.kuali.rice.krad.data.PersistenceOption;
028
029import static org.kuali.rice.core.api.criteria.PredicateFactory.*;
030
031/**
032 * JPA implementation of the EDOcLiteDAO
033 *
034 * @author Kuali Rice Team (rice.collab@kuali.org)
035 */
036public class EDocLiteDAOJpaImpl implements EDocLiteDAO {
037
038    /** static value for active indicator */
039    private static final String ACTIVE_IND_CRITERIA = "activeInd";
040
041    /** static value for name */
042    private static final String NAME_CRITERIA = "name";
043
044    //** static value for edl name */
045    private static final String EDL_NAME = "edlName";
046
047    /** static value for UPPER */
048    private static final String UPPER = "UPPER";
049
050    /** static value for definition */
051    private static final String DEFINITION = "definition";
052
053    /** static value for style */
054    private static final String STYLE = "style";
055
056    /** Service that persists data to and from the underlying datasource. */
057    private DataObjectService dataObjectService;
058
059    /**
060     * Returns the data object service.
061     * @return the {@link DataObjectService}
062     */
063    public DataObjectService getDataObjectService() {
064        return this.dataObjectService;
065    }
066
067    /**
068     *
069     * @see #getDataObjectService()
070     */
071    public void setDataObjectService(DataObjectService dataObjectService) {
072        this.dataObjectService = dataObjectService;
073    }
074
075    /**
076     * {@inheritDoc}
077     */
078    @Override
079    public EDocLiteDefinition saveEDocLiteDefinition(final EDocLiteDefinition edocLiteData) {
080        return this.dataObjectService.save(edocLiteData, PersistenceOption.FLUSH);
081    }
082
083    /**
084     * {@inheritDoc}
085     */
086    @Override
087    public EDocLiteAssociation saveEDocLiteAssociation(final EDocLiteAssociation assoc) {
088        return this.dataObjectService.save(assoc, PersistenceOption.FLUSH);
089    }
090
091    /**
092     * {@inheritDoc}
093     */
094    @Override
095    public EDocLiteDefinition getEDocLiteDefinition(final String defName) {
096        QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
097        criteria.setPredicates(equal(NAME_CRITERIA, defName), equal(ACTIVE_IND_CRITERIA, Boolean.TRUE));
098        List<EDocLiteDefinition> edls = this.dataObjectService.findMatching(EDocLiteDefinition.class,
099                criteria.build()).getResults();
100        if (null != edls && !edls.isEmpty()) {
101            return edls.get(0);
102        } else {
103            return null;
104        }
105    }
106
107    /**
108     * {@inheritDoc}
109     */
110    @Override
111    public EDocLiteAssociation getEDocLiteAssociation(final String docTypeName) {
112        QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
113        criteria.setPredicates(equal(EDL_NAME, docTypeName), equal(ACTIVE_IND_CRITERIA, Boolean.TRUE));
114        List<EDocLiteAssociation> edls = this.dataObjectService.findMatching(EDocLiteAssociation.class,
115                criteria.build()).getResults();
116
117        if (null != edls && !edls.isEmpty()) {
118            return edls.get(0);
119        } else {
120            return null;
121        }
122    }
123
124    /**
125     * {@inheritDoc}
126     */
127    @Override
128    public List<String> getEDocLiteDefinitions() {
129        QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
130        criteria.setPredicates(equal(ACTIVE_IND_CRITERIA, Boolean.TRUE));
131        List<EDocLiteDefinition> defs = this.dataObjectService.findMatching(EDocLiteDefinition.class,
132                criteria.build()).getResults();
133        ArrayList<String> names = new ArrayList<String>(defs.size());
134        if (!defs.isEmpty()) {
135            for (EDocLiteDefinition def : defs) {
136                names.add(def.getName());
137            }
138        }
139
140        return names;
141    }
142
143    /**
144     * {@inheritDoc}
145     */
146    @Override
147    public List<EDocLiteAssociation> getEDocLiteAssociations() {
148        QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
149        criteria.setPredicates(equal(ACTIVE_IND_CRITERIA, Boolean.TRUE));
150        return this.dataObjectService.findMatching(EDocLiteAssociation.class, criteria.build()).getResults();
151    }
152
153    /**
154     * {@inheritDoc}
155     */
156    @Override
157    public List<EDocLiteAssociation> search(final EDocLiteAssociation edocLite) {
158        List<Predicate> predicates = new ArrayList<Predicate>();
159        if (edocLite.getActiveInd() != null) {
160            predicates.add(equal(ACTIVE_IND_CRITERIA, edocLite.getActiveInd()));
161        }
162        if (edocLite.getDefinition() != null) {
163            predicates.add(like(UPPER + "(" + DEFINITION + ")", "%" + edocLite.getDefinition().toUpperCase() + "%"));
164        }
165        if (edocLite.getEdlName() != null) {
166            predicates.add(like(UPPER + "(" + EDL_NAME + ")", "%" + edocLite.getEdlName().toUpperCase() + "%"));
167        }
168        if (edocLite.getStyle() != null) {
169            predicates.add(like(UPPER + "(" + STYLE + ")", "%" + edocLite.getStyle().toUpperCase() + "%"));
170        }
171        QueryByCriteria.Builder builder = QueryByCriteria.Builder.create();
172        builder.setPredicates(predicates.toArray(new Predicate[predicates.size()]));
173        return this.dataObjectService.findMatching(EDocLiteAssociation.class, builder.build()).getResults();
174
175    }
176
177    /**
178     * {@inheritDoc}
179     */
180    @Override
181    public EDocLiteAssociation getEDocLiteAssociation(final Long associationId) {
182        return dataObjectService.find(EDocLiteAssociation.class, associationId);
183    }
184}