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