Example usage for javax.persistence PersistenceException getMessage

List of usage examples for javax.persistence PersistenceException getMessage

Introduction

In this page you can find the example usage for javax.persistence PersistenceException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:org.eclipse.jubula.client.core.persistence.ProjectPM.java

/**
 * Load an instance of ProjectPO into the readonly session. The read
 * instance is db identical to the key supplied.
 * used for open project//from w w w .  ja v  a  2s .c o  m
 * 
 * @param project
 *            Look for this instance in the db.
 * @throws PMReadException
 *             if loading from db failed
 * 
 */
public static void loadProjectInROSession(IProjectPO project) throws PMReadException {

    GeneralStorage.getInstance().reset();
    EntityManager s = GeneralStorage.getInstance().getMasterSession();
    s.clear(); // get rid of all session (cached) data
    try {
        preloadData(s, project);

        IProjectPO p = s.find(NodeMaker.getProjectPOClass(), project.getId());
        GeneralStorage.getInstance().setProject(p);
        ParamNameBP.getInstance().initMap();
        ComponentNamesBP.getInstance().init();
    } catch (PersistenceException e) {
        GeneralStorage.getInstance().setProject(null);
        OperationCanceledException cancel = checkForCancel(e);
        if (cancel != null) {
            throw cancel;
        }
        String msg = Messages.CantReadProjectFromDatabase + StringConstants.DOT;
        log.error(Messages.UnexpectedPersistenceErrorIgnored + StringConstants.DOT, e);
        throw new PMReadException(msg + e.getMessage(), MessageIDs.E_CANT_READ_PROJECT);
    } catch (PMException e) {
        String msg = Messages.CouldNotReadParamNamesFromDB + StringConstants.DOT;
        log.error(msg, e);
        throw new PMReadException(msg + e.getMessage(), MessageIDs.E_CANT_READ_PROJECT);
    } catch (JBException e) {
        GeneralStorage.getInstance().setProject(null);
        String msg = Messages.CantReadProjectFromDatabase + StringConstants.DOT;
        log.error(Messages.UnexpectedPersistenceErrorIgnored, e);
        throw new PMReadException(msg + e.getMessage(), MessageIDs.E_CANT_READ_PROJECT);
    }
}

From source file:org.eclipse.jubula.client.core.persistence.ProjectPM.java

/**
 * @see ProjectPM#getReusedProjectsForProject(EntityManager, long)
 * @param projectID/*from w w  w.  ja va2s .c  o m*/
 *            Object id for the project to be used
 * @return A List of IReusedProjectPOs for the submitted projectID. The Set
 *         may be empty. Since the session is closed after this call the
 *         resulting entities are detached.
 */
public static List<IReusedProjectPO> getReusedProjectsForProject(long projectID) throws PMException {
    EntityManager session = null;
    try {
        session = Persistor.instance().openSession();
        return getReusedProjectsForProject(session, projectID);
    } catch (PersistenceException e) {
        log.error(Messages.PersistenceLoadFailed, e);
        throw new PMException(e.getMessage(), MessageIDs.E_PERSISTENCE_LOAD_FAILED);
    } finally {
        Persistor.instance().dropSessionWithoutLockRelease(session);
    }

}

From source file:org.eclipse.jubula.client.core.persistence.ProjectPM.java

/**
 * @see ProjectPM#getReusedProjectsForProject(EntityManager, long)
 * @param projectID//  w w  w  . j av  a  2s. c  o m
 *            Object id for the project to be used
 * @return A List of IReusedProjectPOs for the submitted projectID. The Set
 *         may be empty. 
 */
public static List<IReusedProjectPO> getReusedProjectsForProjectRO(long projectID) throws PMException {
    EntityManager session = GeneralStorage.getInstance().getMasterSession();
    try {
        return getReusedProjectsForProject(session, projectID);
    } catch (PersistenceException e) {
        log.error(Messages.PersistenceLoadFailed, e);
        throw new PMException(e.getMessage(), MessageIDs.E_PERSISTENCE_LOAD_FAILED);
    }
}

From source file:org.eclipse.jubula.client.core.persistence.ProjectPM.java

/**
 * Handles a <code>PersistenceException</code>.
 * /*from   www. j  av a2s  .  c  o m*/
 * @param mapperList The Parameter Name mapping list.
 * @param s The session.
 * @param tx The transaction.
 * @param e The exception.
 * @throws PMException if the rollback of the transaction fails.
 * @throws InterruptedException if the cause of the given exception was 
 *                              that the operation was canceled.
 * @throws PMSaveException wrapper for the Persistence exception.
 */
private static void handlePersistenceException(List<INameMapper> mapperList, EntityManager s,
        EntityTransaction tx, PersistenceException e)
        throws PMException, InterruptedException, PMSaveException {

    if (tx != null) {
        Persistor.instance().rollbackTransaction(s, tx);
    }
    if (e.getCause() instanceof InterruptedException) {

        GeneralStorage.getInstance().reset();
        for (INameMapper mapper : mapperList) {
            mapper.clearAllNames();
        }
        // Operation was canceled.
        throw new InterruptedException();
    }
    String msg = Messages.CantAttachProject + StringConstants.DOT;
    throw new PMSaveException(msg + e.getMessage(), MessageIDs.E_ATTACH_PROJECT);
}

From source file:org.eclipse.jubula.client.core.persistence.ProjectPM.java

/**
 * Persists the given project to the DB. This is performed in a new session.
 * When this method returns, the project will not be attached to any session.
 * @param proj ProjectPO to be saved.//w  ww . jav  a 2  s.  c  o m
 * @param newProjectName
 *            name part of the ProjectNamePO. If there is no new name, this
 *            parameter must be null (same project, different version)
 * @param mapperList a List of INameMapper to persist names (Parameter).
 * @param compNameBindingList a List of Component Name mappers to persist 
 *                            names (Component).
 * @throws PMException in case of any db error
 * @throws ProjectDeletedException if project is already deleted
 * @throws InterruptedException if the operation is canceled
 */
public static void saveProject(IProjectPO proj, String newProjectName, List<INameMapper> mapperList,
        List<IWritableComponentNameMapper> compNameBindingList)
        throws PMException, ProjectDeletedException, InterruptedException {

    final EntityManager saveSession = Persistor.instance().openSession();
    EntityTransaction tx = null;
    try {
        tx = Persistor.instance().getTransaction(saveSession);

        saveSession.persist(proj);
        proj.setParentProjectId(proj.getId());

        saveSession.flush();
        if (newProjectName != null) {
            ProjectNameBP.getInstance().setName(saveSession, proj.getGuid(), newProjectName);
        }
        ProjectNameBP.getInstance().storeTransientNames(saveSession);
        for (INameMapper mapper : mapperList) {
            mapper.persist(saveSession, proj.getId());
        }
        for (IWritableComponentNameMapper compNameBinding : compNameBindingList) {
            CompNamePM.flushCompNames(saveSession, proj.getId(), compNameBinding);
        }
        Persistor.instance().commitTransaction(saveSession, tx);
        for (INameMapper mapper : mapperList) {
            mapper.updateStandardMapperAndCleanup(proj.getId());
        }
        for (IComponentNameMapper compNameCache : compNameBindingList) {
            compNameCache.getCompNameCache().updateStandardMapperAndCleanup(proj.getId());
        }
    } catch (PersistenceException e) {
        if (tx != null) {
            Persistor.instance().rollbackTransaction(saveSession, tx);
        }
        if (e.getCause() instanceof InterruptedException) {
            // Operation was canceled.
            throw new InterruptedException();
        }
        String msg = Messages.CantSaveProject + StringConstants.DOT;
        throw new PMSaveException(msg + e.getMessage(), MessageIDs.E_ATTACH_PROJECT);
    } catch (IncompatibleTypeException ite) {
        if (tx != null) {
            Persistor.instance().rollbackTransaction(saveSession, tx);
        }
        String msg = Messages.CantSaveProject + StringConstants.DOT;
        throw new PMSaveException(msg + ite.getMessage(), MessageIDs.E_ATTACH_PROJECT);
    } finally {
        Persistor.instance().dropSession(saveSession);
    }
}

From source file:org.eclipse.jubula.client.core.persistence.ProjectPM.java

/**
 * @param guid//from   w  w w.j av a 2 s  .  co  m
 *            The GUID to search.
 * @return a String representing the highest version number for this project
 *         GUID
 */
public static synchronized String findHighestVersionNumber(String guid) throws JBException {

    EntityManager session = null;
    try {
        session = Persistor.instance().openSession();
        Query query = session.createQuery("select project from ProjectPO project" //$NON-NLS-1$
                + " inner join fetch project.properties where project.guid = :guid " //$NON-NLS-1$
                + "order by project.properties.majorNumber desc, project.properties.minorNumber desc"); //$NON-NLS-1$
        query.setParameter("guid", guid); //$NON-NLS-1$
        query.setMaxResults(1);
        final List projList = query.getResultList();
        if (projList.isEmpty()) {
            return StringConstants.EMPTY;
        }
        IProjectPO project = (IProjectPO) projList.get(0);
        return project.getMajorProjectVersion() + StringConstants.DOT + project.getMinorProjectVersion();
    } catch (PersistenceException e) {
        log.error(Messages.PersistenceLoadFailed, e);
        throw new JBException(e.getMessage(), MessageIDs.E_PERSISTENCE_LOAD_FAILED);
    } finally {
        Persistor.instance().dropSessionWithoutLockRelease(session);
    }
}

From source file:org.eclipse.jubula.client.core.persistence.ProjectPM.java

/**
 * Returns a list of all projects that can be used by a project with the 
 * given properties. This query is executed in a new session, so the 
 * objects in the returned list are detached from their session.
 * @param guid The GUID of the project that may use projects from the 
 *             returned list.//  w  w w  .  j av  a  2  s. c o m
 * @param majorVersionNumber The major version number of the project 
 *                           wishing to reuse.
 * @param minorVersionNumber The minor version number of the project 
 *                           wishing to reuse.
 * @param toolkit The toolkit of the project that may use projects from the 
 *             returned list.
 * @param toolkitLevel The toolkit level of the project that may use 
 *                     projects from the returned list.
 * @return a list of all reusable projects that could be used by a project 
 *         with the given properties.
 */
@SuppressWarnings("unchecked")
public static synchronized List<IProjectPO> findReusableProjects(String guid, int majorVersionNumber,
        int minorVersionNumber, String toolkit, String toolkitLevel) throws JBException {
    EntityManager session = null;
    try {
        session = Persistor.instance().openSession();
        Query query = session.createQuery("select project from ProjectPO project" //$NON-NLS-1$
                + " inner join fetch project.properties where project.properties.isReusable = :isReusable" //$NON-NLS-1$
                + " and project.guid != :guid"); //$NON-NLS-1$
        query.setParameter("isReusable", true); //$NON-NLS-1$
        query.setParameter("guid", guid); //$NON-NLS-1$
        List<IProjectPO> projects = query.getResultList();
        Iterator<IProjectPO> iter = projects.iterator();
        while (iter.hasNext()) {
            IProjectPO project = iter.next();
            String reusedToolkit = project.getToolkit();
            try {
                String reusedToolkitLevel = ToolkitSupportBP.getToolkitLevel(reusedToolkit);
                if (!(reusedToolkit.equals(toolkit) || ToolkitUtils.doesToolkitInclude(toolkit, reusedToolkit)
                        || ToolkitUtils.isToolkitMoreConcrete(toolkitLevel, reusedToolkitLevel))) {
                    iter.remove();
                }
            } catch (ToolkitPluginException tpe) {
                StringBuilder msg = new StringBuilder();
                msg.append(Messages.Project);
                msg.append(StringConstants.SPACE);
                msg.append(project.getName());
                msg.append(StringConstants.SPACE);
                msg.append(Messages.CouldNotBeLoadedAnUnavailableToolkitPlugin);
                msg.append(StringConstants.DOT);
                // Plugin for toolkit could not be loaded.
                log.error(msg.toString());
                // Remove the project using the unavailable toolkit
                // from the available projects list.
                iter.remove();
            }
        }
        // We have a list of reusable projects with compatible toolkits.
        // Now we need to remove from the list any projects that would lead 
        // to circular dependencies. 
        Set<IProjectPO> checkedProjects = new HashSet<IProjectPO>();
        Set<IProjectPO> illegalProjects = new HashSet<IProjectPO>();

        IProjectPO givenProject = loadProjectByGuidAndVersion(guid, majorVersionNumber, minorVersionNumber);

        if (givenProject == null) {
            log.debug(Messages.TriedFindProjectsForNonExistantProject);
            return new ArrayList<IProjectPO>();
        }

        // Project can't reuse itself
        illegalProjects.add(givenProject);
        checkedProjects.add(givenProject);

        for (IProjectPO proj : projects) {
            findIllegalProjects(proj, checkedProjects, illegalProjects, null);
        }

        projects.removeAll(illegalProjects);

        return projects;
    } catch (PersistenceException e) {
        log.error(Messages.PersistenceLoadFailed, e);
        throw new JBException(e.getMessage(), MessageIDs.E_PERSISTENCE_LOAD_FAILED);
    } finally {
        Persistor.instance().dropSessionWithoutLockRelease(session);
    }
}

From source file:org.eclipse.jubula.client.core.persistence.ProjectPM.java

/**
 * @return The project for the given (database) ID
 * @param projectId//from  ww  w  .j  av  a2 s  .c o  m
 *            (database) ID of the project to load
 * @param session
 *      The session to use for loading. The returned project will be 
 *      attached to this session. It is the responsibility of the
 *      caller to close the session.
 * @throws JBException
 *             if the session cannot be loaded.
 */
public static synchronized IProjectPO loadProjectById(Long projectId, EntityManager session)
        throws JBException {

    if (projectId == null) {
        return null;
    }

    try {
        Query query = session.createQuery("select project from ProjectPO project" //$NON-NLS-1$
                + " where project.id = :id"); //$NON-NLS-1$
        query.setParameter("id", projectId); //$NON-NLS-1$
        try {
            return (IProjectPO) query.getSingleResult();
        } catch (NoResultException nre) {
            return null;
        }
    } catch (PersistenceException e) {
        OperationCanceledException oce = checkForCancel(e);
        if (oce != null) {
            throw oce;
        }
        log.error(Messages.PersistenceLoadFailed, e);
        throw new JBException(e.getMessage(), MessageIDs.E_PERSISTENCE_LOAD_FAILED);
    }
}

From source file:org.eclipse.jubula.client.core.persistence.ProjectPM.java

/**
 * Returns a list of project IDs, each ID represents a project currently in 
 * the database that reuses the project with the given information.
 * //  www  .  j  av  a 2s. co m
 * @param guid GUID of the reused project.
 * @param majorVersion Major version number of the reused project.
 * @param minorVersion Minor version number of the reused project.
 * @return the IDs of all projects that reuse the project with the given
 *         information.
 */
@SuppressWarnings("unchecked")
public static synchronized List<Long> findIdsThatReuse(String guid, int majorVersion, int minorVersion)
        throws JBException {

    EntityManager session = null;
    List<Long> hits;
    try {
        session = Persistor.instance().openSession();
        Query query = session.createQuery("select reusedProject.hbmParentProjectId from ReusedProjectPO" //$NON-NLS-1$
                + " as reusedProject where reusedProject.projectGuid = :projectGuid and reusedProject.majorNumber = :majorNumber" //$NON-NLS-1$
                + " and reusedProject.minorNumber = :minorNumber"); //$NON-NLS-1$
        query.setParameter("projectGuid", guid); //$NON-NLS-1$
        query.setParameter("majorNumber", majorVersion); //$NON-NLS-1$
        query.setParameter("minorNumber", minorVersion); //$NON-NLS-1$
        hits = query.getResultList();
    } catch (PersistenceException e) {
        log.error(Messages.PersistenceLoadFailed, e);
        throw new JBException(e.getMessage(), MessageIDs.E_PERSISTENCE_LOAD_FAILED);
    } finally {
        Persistor.instance().dropSessionWithoutLockRelease(session);
    }
    return hits;

}

From source file:org.eclipse.jubula.client.core.persistence.ProjectPM.java

/**
 * Gets the GUID of the project with the given ID.
 * @param projId the ID of the project//from w w w  .j a  va  2 s  . co m
 * @return the GUID of the project with the given ID or null if
 * no project with the given ID was found.
 * @throws JBException if the session cannot be loaded or closed.
 */
public static final synchronized String getGuidOfProjectId(Long projId) throws JBException {
    String cachedGuid = guidCache.get(projId);
    if (cachedGuid != null) {
        return cachedGuid;
    }
    EntityManager session = null;
    String projGuid = null;
    try {
        session = Persistor.instance().openSession();
        final Query query = session
                .createQuery("select project.guid from ProjectPO project where project.id = :projectID"); //$NON-NLS-1$
        query.setParameter("projectID", projId); //$NON-NLS-1$
        projGuid = (String) query.getSingleResult();
    } catch (NoResultException nre) {
        // No result found. Fall through to return null.
    } catch (PersistenceException e) {
        throw new JBException(e.getMessage(), MessageIDs.E_PERSISTENCE_LOAD_FAILED);
    } finally {
        Persistor.instance().dropSessionWithoutLockRelease(session);
    }
    guidCache.put(projId, projGuid);
    return projGuid;
}