View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.edl.impl.dao.impl;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.kuali.rice.core.api.criteria.Predicate;
22  import org.kuali.rice.core.api.criteria.QueryByCriteria;
23  import org.kuali.rice.edl.impl.bo.EDocLiteAssociation;
24  import org.kuali.rice.edl.impl.bo.EDocLiteDefinition;
25  import org.kuali.rice.edl.impl.dao.EDocLiteDAO;
26  import org.kuali.rice.krad.data.DataObjectService;
27  import org.kuali.rice.krad.data.PersistenceOption;
28  
29  import static org.kuali.rice.core.api.criteria.PredicateFactory.*;
30  
31  /**
32   * JPA implementation of the EDOcLiteDAO
33   *
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   */
36  public class EDocLiteDAOJpaImpl implements EDocLiteDAO {
37      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(EDocLiteDAOJpaImpl.class);
38  
39      /** static value for active indicator */
40      private static final String ACTIVE_IND_CRITERIA = "activeInd";
41  
42      /** static value for name */
43      private static final String NAME_CRITERIA = "name";
44  
45      //** static value for edl name */
46      private static final String EDL_NAME = "edlName";
47  
48      /** static value for UPPER */
49      private static final String UPPER = "UPPER";
50  
51      /** static value for definition */
52      private static final String DEFINITION = "definition";
53  
54      /** static value for style */
55      private static final String STYLE = "style";
56  
57      /** Service that persists data to and from the underlying datasource. */
58      private DataObjectService dataObjectService;
59  
60      /**
61       * Returns the data object service.
62       * @return the {@link DataObjectService}
63       */
64      public DataObjectService getDataObjectService() {
65          return this.dataObjectService;
66      }
67  
68      /**
69       *
70       * @see #getDataObjectService()
71       */
72      public void setDataObjectService(DataObjectService dataObjectService) {
73          this.dataObjectService = dataObjectService;
74      }
75  
76      /**
77       * {@inheritDoc}
78       */
79      @Override
80      public EDocLiteDefinition saveEDocLiteDefinition(final EDocLiteDefinition edocLiteData) {
81          return this.dataObjectService.save(edocLiteData, PersistenceOption.FLUSH);
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      public EDocLiteAssociation saveEDocLiteAssociation(final EDocLiteAssociation assoc) {
89          return this.dataObjectService.save(assoc, PersistenceOption.FLUSH);
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      public EDocLiteDefinition getEDocLiteDefinition(final String defName) {
97          QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
98          criteria.setPredicates(equal(NAME_CRITERIA, defName), equal(ACTIVE_IND_CRITERIA, Boolean.TRUE));
99          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 }