Example usage for javax.persistence EntityManager merge

List of usage examples for javax.persistence EntityManager merge

Introduction

In this page you can find the example usage for javax.persistence EntityManager merge.

Prototype

public <T> T merge(T entity);

Source Link

Document

Merge the state of the given entity into the current persistence context.

Usage

From source file:nl.b3p.kaartenbalie.service.GroupParser.java

public static void addAllLayersToGroup(Organization org, ServiceProvider sp, EntityManager em)
        throws Exception {
    Set wmsLayers = new HashSet();

    Set<Layer> orgWmsLayerSet = org.getLayers();
    for (Layer l : orgWmsLayerSet) {
        ServiceProvider layerSp = l.getServiceProvider();

        if (!layerSp.getAbbr().equals(sp.getAbbr())) {
            wmsLayers.add(l);//w w  w  .  j  a v a 2  s.c o m
        }
    }

    Set<Layer> selectedLayers = sp.getAllLayers();
    for (Layer l : selectedLayers) {
        wmsLayers.add(l);
    }

    org.setLayers(wmsLayers);

    em.merge(org);
    em.flush();
}

From source file:entity.files.SYSFilesTools.java

public static boolean deleteFile(SYSFiles sysfile) {
    boolean success = false;
    EntityManager em = OPDE.createEM();

    try {/*from  w w  w  .j  a va2  s  .  c om*/
        em.getTransaction().begin();
        SYSFiles mySYSFile = em.merge(sysfile);
        em.remove(mySYSFile);
        em.getTransaction().commit();
        success = true;
    } catch (OptimisticLockException ole) {
        OPDE.warn(ole);
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (ole.getMessage().indexOf("Class> entity.info.Resident") > -1) {
            OPDE.getMainframe().emptyFrame();
            OPDE.getMainframe().afterLogin();
        }

        OPDE.getDisplayManager().addSubMessage(DisplayManager.getLockMessage());
        success = false;
    } catch (Exception ex) {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        success = false;
    } finally {
        em.close();
    }

    if (success) {
        try {
            FileTransferClient ftp = getFTPClient();
            ftp.deleteFile(sysfile.getRemoteFilename());
            ftp.disconnect();
            OPDE.info("DELETING FILE FROM FTPSERVER: " + sysfile.getFilename() + " (" + sysfile.getMd5() + ")");
        } catch (Exception e) {
            OPDE.error(e);
            success = false;
        }
    }

    return success;
}

From source file:entity.files.SYSFilesTools.java

public static void detachFiles(SYSFiles[] files, SYSFilesContainer sysFilesContainer) {

    FileTransferClient ftp = getFTPClient();

    if (ftp != null) {
        EntityManager em = OPDE.createEM();
        try {//from  www  .  j ava 2  s  .  co  m
            em.getTransaction().begin();

            for (SYSFiles sysfile : files) {
                SYSFilesLink link = em.merge(sysFilesContainer.detachFile(sysfile));
                em.remove(link);
            }

            em.getTransaction().commit();
        } catch (OptimisticLockException ole) {
            OPDE.warn(ole);
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            if (ole.getMessage().indexOf("Class> entity.info.Resident") > -1) {
                OPDE.getMainframe().emptyFrame();
                OPDE.getMainframe().afterLogin();
            }
            OPDE.getDisplayManager().addSubMessage(DisplayManager.getLockMessage());
        } catch (Exception ex) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            OPDE.fatal(ex);
        } finally {
            em.close();
            try {
                ftp.disconnect();
            } catch (Exception e) {
                OPDE.error(e);
            }
        }
    }
}

From source file:nl.b3p.kaartenbalie.service.GroupParser.java

public static void addAllLayersToGroup(Organization org, WfsServiceProvider sp, EntityManager em)
        throws Exception {
    log.info("Updating WFS rights for :" + org.getName());

    Set wfsLayers = new HashSet();

    Set<WfsLayer> orgWfsLayerSet = org.getWfsLayers();
    for (WfsLayer l : orgWfsLayerSet) {
        WfsServiceProvider layerSp = l.getWfsServiceProvider();

        if (!layerSp.getAbbr().equals(sp.getAbbr())) {
            wfsLayers.add(l);/*from  w ww  .  j  av a  2  s. com*/

            log.info("Org wfs layer :" + l.getName());
        }
    }

    Set<WfsLayer> selectedLayers = sp.getWfsLayers();
    for (WfsLayer l : selectedLayers) {
        wfsLayers.add(l);

        log.info("Wfs layer :" + l.getName());
    }

    org.setWfsLayers(wfsLayers);

    em.merge(org);
    em.flush();
}

From source file:com.siberhus.ngai.core.CrudHelper.java

@SuppressWarnings("unchecked")
public final static Object save(EntityManager em, Object model) {
    if (em == null) {
        throw new IllegalArgumentException("EntityManager is null");
    }//from w  ww .j  ava 2 s .com
    if (model instanceof IModel) {
        if (((IModel<Serializable>) (model)).isNew()) {
            em.persist(model);
            return model;
        }
    } else {
        Object id = null;
        try {
            id = PropertyUtils.getProperty(model, "id");
        } catch (Exception e) {
            throw new NgaiRuntimeException("Unable to find property 'id' fom model: " + model, e);
        }
        if (id == null) {
            em.persist(model);
            return model;
        }
    }
    return em.merge(model);
}

From source file:org.sigmah.server.endpoint.export.sigmah.handler.ProjectReportModelHandler.java

/**
 * Save the quality criterion passed in argument.
 * /*  w w w .  j  av a 2 s .  c om*/
 * @param qualityCriterion
 *            the quality criterion to save
 * @param em
 *            the entity manager
 */
private static void saveKeyQuestionQualityCriterion(QualityCriterion qualityCriterion, EntityManager em) {
    List<QualityCriterion> qualityCriterions = qualityCriterion.getSubCriteria();
    QualityFramework qualityFramework = qualityCriterion.getQualityFramework();
    if (qualityCriterions != null || qualityFramework != null) {
        qualityCriterion.setSubCriteria(null);
        qualityCriterion.setQualityFramework(null);
        em.persist(qualityCriterion);
        for (QualityCriterion criterion : qualityCriterions) {
            saveKeyQuestionQualityCriterion(criterion, em);
        }
        qualityCriterion.setSubCriteria(qualityCriterions);
        em.persist(qualityFramework);
        qualityCriterion.setQualityFramework(qualityFramework);
        em.merge(qualityCriterion);
    } else {
        em.persist(qualityCriterion);
    }
}

From source file:uk.co.threeonefour.ifictionary.web.user.dao.JpaUserDao.java

@Transactional
@Override
public User update(User user) {

    EntityManager em = entityManager;
    em.merge(user);
    em.flush();
    return user;
}

From source file:com.gerenciaProyecto.DaoImple.UsuarioDaoImpl.java

@Override
@Transactional/* w w  w.  j  a  va  2  s. c  o m*/
public void modificar(Usuario usuario) {
    EntityManager em = getEntityManager();
    try {
        em.merge(usuario);
        em.flush();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.gerenciaProyecto.DaoImple.ProductoDaoImpl.java

@Override
@Transactional/*  w  w  w  . ja v a 2  s  .com*/
public void modificar(Producto producto) {
    EntityManager em = getEntityManager();
    try {
        em.merge(producto);
        em.flush();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:entity.files.SYSFilesTools.java

/**
 * <code>putFile(String)</code> speichert eine Datei auf dem FTP Server ab und erstellt eine SYSFiles EntityBean. Die Methode geht dabei wie folgt vor:
 * <ol>/*w  w  w.  j  a  v  a2 s.c o  m*/
 * <li>Zuerst wird der MD5 der zu speichernden Datei berechnet.</li>
 * <li>Anhand dieser MD5 Signatur wird ermittelt, ob es schon einen entsprechenden Eintrag in der Datenbank gibt.
 * <ul>
 * <li>Wenn ja, dann wird einfach der PK zurckgegeben. Nochmal speichern braucht man das ja nicht. <b>RETURN</b></li>
 * <li>Wenn nicht, dann geht die Bearbeitung weiter.</li>
 * </ul>
 * <li>Erstellen einer SYSFiles EB</li>
 * <li>Senden der Datei an den FTP Server</li>
 * <li>Wenn die letzten beiden Schritte erfolgreich waren, dann wird die neue EB als Ergebnis zurck gegeben. null, bei Fehler.</li>
 * </ol>
 *
 * @param file File Objekt der zu speichernden Datei
 * @return EB der neuen Datei. null bei Fehler.
 */
private static SYSFiles putFile(EntityManager em, FileTransferClient ftp, File file) throws Exception {
    SYSFiles sysfile = null;

    String md5 = SYSTools.getMD5Checksum(file);
    Query query = em.createQuery("SELECT s FROM SYSFiles s WHERE s.md5 = :md5");
    query.setParameter("md5", md5);

    ArrayList<SYSFiles> alreadyExistingFiles = new ArrayList<SYSFiles>(query.getResultList());

    // Gibts die Datei schon ?
    if (alreadyExistingFiles.isEmpty()) { // nein, noch nicht
        sysfile = em.merge(new SYSFiles(file.getName(), md5, new Date(file.lastModified()), file.length(),
                OPDE.getLogin().getUser()));
        //            FileInputStream fis = new FileInputStream(file);
        ftp.uploadFile(file.getPath(), sysfile.getRemoteFilename());

        //            ftp.storeF.storeFile(file.getPath(),sysfile.getRemoteFilename());
        OPDE.info(
                SYSTools.xx("misc.msg.upload") + ": " + sysfile.getFilename() + " (" + sysfile.getMd5() + ")");
        //            fis.close();
    } else { // Ansonsten die bestehende Datei zurckgeben

        sysfile = alreadyExistingFiles.get(0);

        // Does the User own this file already ?
        //            for (SYSFiles mySYSfile : alreadyExistingFiles) {
        //                if (mySYSfile.getResident().equals(resident)) {
        //                    sysfile = mySYSfile;
        //                    break;
        //                }
        //            }
        //            if (sysfile == null) {
        //                sysfile = em.merge(new SYSFiles(file.getName(), md5, new Date(file.lastModified()), file.length(), OPDE.getLogin().getUser(), resident));
        //            }
    }

    return sysfile;
}