@Override public DagDto saveDag(DagDto dag) { try { Dag dg = Convert.DtoToDag(dag, em); for (Open op : dg.getOpens()) { if (op.getId() != null) em.merge(op); else em.persist(op); } ArrayList<Integer> nw = new ArrayList<Integer>(); for (Dienst dienst : dg.getDiensts()) { System.out.println("Dienst: " + dienst.getZaaldienst().getNaam()); if (dienst.getId() != null) em.merge(dienst); else em.persist(dienst); nw.add(dienst.getId()); } Query rs; if (nw.size() > 0) { rs = em.createQuery("SELECT x FROM Dienst as x WHERE dag = :dag AND id NOT IN(:dienst)"); rs.setParameter("dienst", nw); } else { rs = em.createQuery("SELECT x FROM Dienst as x WHERE dag = :dag"); } rs.setParameter("dag", dg); @SuppressWarnings("unchecked") List<Dienst> tp = rs.getResultList(); for (Dienst del : tp) { System.out.println("Deleting " + del.getZaaldienst().getNaam()); em.remove(del); } em.merge(dg); return Convert.DagToDto(dg); } catch (Exception e) { API.createIssue( "Service SQL exception in saveDag", "A service SQL exception has encountered", e); throw new RuntimeException(e); } }
@Override public DagDto getDagByDate(DagRequest datum) { try { System.out.println("Getting data for datum: " + datum); Query result = em.createQuery( "select X from Dag as X where dag = :dag and maand = :maand and jaar = :jaar"); result.setParameter("dag", datum.getDag()); result.setParameter("maand", datum.getMaand()); result.setParameter("jaar", datum.getJaar()); try { Dag zt = (Dag) result.getSingleResult(); return Convert.DagToDto(zt); } catch (NoResultException e) { } System.out.println("Date not exists yet"); // No date found yet, lets create a standard date (And persist it!) Dag dag = new Dag(); Query rs = em.createQuery("select X from Type as X"); dag.setDag(datum.getDag()); dag.setMaand(datum.getMaand()); dag.setJaar(datum.getJaar()); em.persist(dag); @SuppressWarnings("unchecked") List<Type> tp = rs.getResultList(); Set<Open> op = new HashSet<Open>(); for (Type deel : tp) { Open open = new Open(); open.setOpen(false); open.setType(deel); open.setDag(dag); op.add(open); em.persist(open); } dag.setOpens(op); em.persist(dag); System.out.println("size: " + dag.getOpens().size()); return Convert.DagToDto(dag); } catch (Exception e) { API.createIssue( "Service SQL exception in getDagByDate", "A service SQL exception has encountered", e); throw new RuntimeException(e); } }