| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 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 |  |   | 
  | 34 |  |   | 
  | 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 |  |   | 
  | 48 |  |   | 
  | 49 |  |   | 
  | 50 |  |   | 
  | 51 |  |   | 
  | 52 |  |   | 
  | 53 |  |   | 
  | 54 |  |   | 
  | 55 |  |   | 
  | 56 |  |   | 
  | 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 |  |   | 
  | 75 |  |   | 
  | 76 |  |   | 
  | 77 |  |   | 
  | 78 |  |   | 
  | 79 |  |   | 
  | 80 |  |   | 
  | 81 |  |   | 
  | 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 |  |   | 
  | 89 |  |   | 
  | 90 |  |   | 
  | 91 |  |   | 
  | 92 |  |   | 
  | 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 |  |   | 
  | 109 |  |   | 
  | 110 |  |   | 
  | 111 |  |   | 
  | 112 |  |   | 
  | 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 |  |  } |