Coverage Report - org.kuali.student.common.ui.server.applicationstate.dao.impl.ApplicationStateDaoImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ApplicationStateDaoImpl
76%
20/26
50%
2/4
2.6
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  */
 15  
 
 16  
 package org.kuali.student.common.ui.server.applicationstate.dao.impl;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.Collection;
 20  
 import java.util.List;
 21  
 
 22  
 import javax.persistence.EntityManager;
 23  
 import javax.persistence.PersistenceContext;
 24  
 import javax.persistence.Query;
 25  
 
 26  
 import org.kuali.student.common.dao.impl.AbstractSearchableCrudDaoImpl;
 27  
 import org.kuali.student.common.exceptions.AlreadyExistsException;
 28  
 import org.kuali.student.common.exceptions.DoesNotExistException;
 29  
 import org.kuali.student.common.ui.server.applicationstate.dao.ApplicationStateDao;
 30  
 import org.kuali.student.common.ui.server.applicationstate.entity.ApplicationState;
 31  
 
 32  
 /**
 33  
  * This data access class stores the GUI (page, section, widget, etc.) 
 34  
  * application states as key value pairs in a database.
 35  
  */
 36  10
 public class ApplicationStateDaoImpl extends AbstractSearchableCrudDaoImpl implements ApplicationStateDao {
 37  
 
 38  
         private final static String DEFAULT_USER_ID = "APPLICATION";
 39  
         
 40  
         @PersistenceContext(unitName = "ApplicationState")
 41  
         @Override
 42  
         public void setEm(EntityManager em) {
 43  10
                 super.setEm(em);
 44  10
         }
 45  
 
 46  
         /**
 47  
          * Gets an application states by <code>applicationId</code>, 
 48  
          * <code>referenceKey</code>, <code>referenceType</code> and 
 49  
          * <code>userId</code>.
 50  
          * 
 51  
          * @param applicationId Application id
 52  
          * @param referenceKey Reference key
 53  
          * @param referenceType Reference type
 54  
          * @param userId User id
 55  
          * @return An application state
 56  
          * @throws DoesNotExistException Thrown if application state does not exist
 57  
          */
 58  
         public ApplicationState getApplicationState(String applicationId, String referenceKey, String referenceType, String userId) throws DoesNotExistException {
 59  23
                 Query query = em.createNamedQuery("ApplicationState.getApplicationStateByAppRefUserId");
 60  23
                 query.setParameter("applicationId", applicationId);
 61  23
                 query.setParameter("referenceKey", referenceKey);
 62  23
                 query.setParameter("referenceType", referenceType);
 63  23
                 query.setParameter("userId", userId);
 64  
 
 65  
                 try {
 66  23
                         ApplicationState appState = (ApplicationState) query.getSingleResult();
 67  11
                         return appState;
 68  12
                 } catch(javax.persistence.NoResultException e) {
 69  12
                         throw new DoesNotExistException("Application state does not exist");
 70  
                 }
 71  
         }
 72  
 
 73  
         /**
 74  
          * Gets an application states by <code>applicationId</code>, 
 75  
          * <code>referenceKey</code> and <code>referenceType</code>.
 76  
          * 
 77  
          * @param applicationId Application id
 78  
          * @param referenceKey Reference key
 79  
          * @param referenceType Reference type
 80  
          * @return A list of application states
 81  
          * @throws DoesNotExistException Thrown if application state does not exist
 82  
          */
 83  
         public ApplicationState getApplicationState(String applicationId, String referenceKey, String referenceType) throws DoesNotExistException {
 84  8
                 return getApplicationState(applicationId, referenceKey, referenceType, DEFAULT_USER_ID);
 85  
         }
 86  
 
 87  
         /**
 88  
          * Creates and returns an application state.
 89  
          * 
 90  
          * @param appState Application state
 91  
          * @return A new application state
 92  
          * @throws AlreadyExistsException Thrown if application state already exists
 93  
          */
 94  
         public ApplicationState createApplicationState(ApplicationState appState) throws AlreadyExistsException {
 95  12
                 if(appState.getUserId() == null) {
 96  7
                         appState.setUserId(DEFAULT_USER_ID);
 97  
                 }
 98  
 
 99  
                 try {
 100  12
                         getApplicationState(appState.getApplicationId(), appState.getReferenceKey(), appState.getReferenceType(), appState.getUserId());
 101  10
                 } catch(DoesNotExistException e) {
 102  10
                         return super.create(appState);
 103  2
                 }
 104  2
                 throw new AlreadyExistsException("Application state already exists");
 105  
         }
 106  
 
 107  
         /**
 108  
          * Creates a collection of application states and returns their ids.
 109  
          * 
 110  
          * @param appStateList collection of application states
 111  
          * @return A list of newly created application state ids
 112  
          * @throws AlreadyExistsException Thrown if application state already exists
 113  
          */
 114  
         public List<String> createApplicationState(Collection<ApplicationState> appStates) throws AlreadyExistsException {
 115  0
                 List<String> newAppStateList = new ArrayList<String>();
 116  0
                 for(ApplicationState appState : appStates) {
 117  0
                         ApplicationState newAppState = createApplicationState(appState);
 118  0
                         newAppStateList.add(newAppState.getId());
 119  0
                 }
 120  0
                 return newAppStateList;
 121  
         }
 122  
 
 123  
 }