Example usage for org.apache.ibatis.session SqlSession commit

List of usage examples for org.apache.ibatis.session SqlSession commit

Introduction

In this page you can find the example usage for org.apache.ibatis.session SqlSession commit.

Prototype

void commit();

Source Link

Document

Flushes batch statements and commits database connection.

Usage

From source file:com.bibisco.manager.LocaleManager.java

License:GNU General Public License

public void saveLocale(String pStrLocale) {

    mLog.debug("Start saveLocale(String)");

    SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryBibisco();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {/*from w ww .  j  ava  2  s  .co  m*/

        PropertiesMapper lPropertiesMapper = lSqlSession.getMapper(PropertiesMapper.class);
        Properties lProperties = new Properties();
        lProperties.setProperty("locale");
        lProperties.setValue(pStrLocale);
        lPropertiesMapper.updateByPrimaryKey(lProperties);
        lSqlSession.commit();

        String[] lStrLocaleSplit = pStrLocale.split("_");
        mLocale = new Locale(lStrLocaleSplit[0], lStrLocaleSplit[1]);

    } catch (Throwable t) {
        lSqlSession.rollback();
        mLog.error(t);
        throw new BibiscoException(t, BibiscoException.SQL_EXCEPTION);
    } finally {
        lSqlSession.close();
    }

    mLog.debug("End saveLocale(String)");
}

From source file:com.bibisco.manager.LocationManager.java

License:GNU General Public License

public static LocationDTO insert(LocationDTO pLocationDTO) {

    mLog.debug("Start insert(LocationDTO)");

    SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryProject();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {//from w w w. j a v a 2  s. c o m
        Locations lLocations = new Locations();
        lLocations.setName(pLocationDTO.getName());
        lLocations.setPosition(pLocationDTO.getPosition());
        lLocations.setNation(pLocationDTO.getNation());
        lLocations.setState(pLocationDTO.getState());
        lLocations.setCity(pLocationDTO.getCity());

        LocationsMapper lLocationsMapper = lSqlSession.getMapper(LocationsMapper.class);
        lLocationsMapper.insertSelective(lLocations);

        pLocationDTO.setIdLocation(lLocations.getIdLocation().intValue());
        pLocationDTO.setTaskStatus(TaskStatus.TODO);

        lSqlSession.commit();

    } catch (Throwable t) {
        mLog.error(t);
        lSqlSession.rollback();
        throw new BibiscoException(t, BibiscoException.SQL_EXCEPTION);
    } finally {
        lSqlSession.close();
    }

    mLog.debug("End insert(LocationDTO)");

    return pLocationDTO;

}

From source file:com.bibisco.manager.LocationManager.java

License:GNU General Public License

public static boolean deleteByPosition(Integer pIntIdPosition) {

    boolean lBlnResult;

    mLog.debug("Start deleteByPosition(Integer)");

    SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryProject();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {/*w  w w .  j a v a  2 s . co m*/
        LocationsExample lLocationsExample = new LocationsExample();
        lLocationsExample.createCriteria().andPositionEqualTo(pIntIdPosition);

        LocationsMapper lLocationsMapper = lSqlSession.getMapper(LocationsMapper.class);

        // get location to delete by position
        Locations lLocations = lLocationsMapper.selectByExample(lLocationsExample).get(0);

        // check if location is referenced
        boolean lBlnIsReferenced = checkIfReferenced(lLocations.getIdLocation().intValue(), lSqlSession);

        // location is not referenced, go on
        if (!lBlnIsReferenced) {

            // delete location
            lLocationsMapper.deleteByPrimaryKey(lLocations.getIdLocation());

            // delete all images
            ImageManager.deleteImagesByElement(lSqlSession, lLocations.getIdLocation().intValue(),
                    ElementType.LOCATIONS);

            // shift down other locations
            lLocationsMapper.shiftDown(pIntIdPosition, Integer.MAX_VALUE);

            lBlnResult = true;
        }
        // location is referenced, stop
        else {
            lBlnResult = false;
        }

        lSqlSession.commit();

    } catch (Throwable t) {
        mLog.error(t);
        lSqlSession.rollback();
        throw new BibiscoException(t, BibiscoException.SQL_EXCEPTION);
    } finally {
        lSqlSession.close();
    }

    return lBlnResult;
}

From source file:com.bibisco.manager.LocationManager.java

License:GNU General Public License

public static void move(Integer pIntSourcePosition, Integer pIntDestPosition) {

    mLog.debug("Start move(Integer, Integer)");

    if (!pIntSourcePosition.equals(pIntDestPosition)) {
        SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance()
                .getSqlSessionFactoryProject();
        SqlSession lSqlSession = lSqlSessionFactory.openSession();
        try {/*from  w  w w. j  a  v  a 2 s. co  m*/

            LocationsExample lLocationsExample = new LocationsExample();
            lLocationsExample.createCriteria().andPositionEqualTo(pIntSourcePosition);

            LocationsMapper lLocationsMapper = lSqlSession.getMapper(LocationsMapper.class);

            // get location to update
            Locations lLocations = lLocationsMapper.selectByExample(lLocationsExample).get(0);

            // update location position with fake position to preserve unique index before shift
            lLocations.setPosition(-1);
            lLocationsMapper.updateByPrimaryKey(lLocations);

            // update other locations' position
            Integer lIntStartPosition;
            Integer lIntEndPosition;
            if (pIntSourcePosition > pIntDestPosition) {
                lIntStartPosition = pIntDestPosition;
                lIntEndPosition = pIntSourcePosition;
                lLocationsMapper.shiftUp(lIntStartPosition, lIntEndPosition);
            } else {
                lIntStartPosition = pIntSourcePosition;
                lIntEndPosition = pIntDestPosition;
                lLocationsMapper.shiftDown(lIntStartPosition, lIntEndPosition);
            }

            // update location position
            lLocations.setPosition(pIntDestPosition);
            lLocationsMapper.updateByPrimaryKey(lLocations);

            lSqlSession.commit();

        } catch (Throwable t) {
            mLog.error(t);
            lSqlSession.rollback();
            throw new BibiscoException(t, BibiscoException.SQL_EXCEPTION);
        } finally {
            lSqlSession.close();
        }
    }

    mLog.debug("End move(Integer, Integer)");
}

From source file:com.bibisco.manager.LocationManager.java

License:GNU General Public License

public static void save(LocationDTO pLocationDTO) {

    mLog.debug("Start update(LocationDTO)");

    SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryProject();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {//www . j av  a2s.co m
        Locations lLocations = new Locations();
        lLocations.setIdLocation(pLocationDTO.getIdLocation().longValue());
        lLocations.setName(pLocationDTO.getName());
        lLocations.setNation(pLocationDTO.getNation());
        lLocations.setState(pLocationDTO.getState());
        lLocations.setCity(pLocationDTO.getCity());
        lLocations.setDescription(pLocationDTO.getDescription());
        if (pLocationDTO.getTaskStatus() != null) {
            lLocations.setTaskStatus(pLocationDTO.getTaskStatus().getValue());
        }

        LocationsMapper lLocationsMapper = lSqlSession.getMapper(LocationsMapper.class);
        lLocationsMapper.updateByPrimaryKeySelective(lLocations);

        lSqlSession.commit();

    } catch (Throwable t) {
        mLog.error(t);
        lSqlSession.rollback();
        throw new BibiscoException(t, BibiscoException.SQL_EXCEPTION);
    } finally {
        lSqlSession.close();
    }

    mLog.debug("End insert(LocationDTO)");
}

From source file:com.bibisco.manager.ProjectManager.java

License:GNU General Public License

public static List<ProjectDTO> loadAll() {

    List<ProjectDTO> lListProjectDTO = null;

    mLog.debug("Start loadAll()");

    // delete projects entries on bibisco db
    ProjectManager.deleteProjectsEntriesOnBibiscoDB();

    // import projects
    importProjectsFromProjectsDirectory();

    SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryBibisco();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {/*ww w  . ja  v  a  2s .c o m*/
        ProjectsMapper lProjectsMapper = lSqlSession.getMapper(ProjectsMapper.class);
        ProjectsExample lProjectsExample = new ProjectsExample();
        lProjectsExample.setOrderByClause("UPPER(name) asc");
        List<Projects> lListProjects = lProjectsMapper.selectByExample(lProjectsExample);

        if (lListProjects != null && lListProjects.size() > 0) {
            lListProjectDTO = new ArrayList<ProjectDTO>();
            for (Projects lProjects : lListProjects) {

                if (projectExists(lProjects.getIdProject())) {
                    ProjectDTO lProjectDTO = new ProjectDTO();
                    lProjectDTO.setIdProject(lProjects.getIdProject());
                    lProjectDTO.setName(lProjects.getName());
                    lListProjectDTO.add(lProjectDTO);
                } else {
                    lProjectsMapper.deleteByPrimaryKey(lProjects.getIdProject());
                }
            }
        }

        lSqlSession.commit();

    } catch (Throwable t) {
        mLog.error(t);
        lSqlSession.rollback();
        throw new BibiscoException(t, BibiscoException.SQL_EXCEPTION);
    } finally {
        lSqlSession.close();
    }

    mLog.debug("End loadAll()");

    return lListProjectDTO;
}

From source file:com.bibisco.manager.ProjectManager.java

License:GNU General Public License

private static void insertIntoProjectDB(ProjectDTO pProjectDTO) {

    mLog.debug("Start insertIntoProjectDB(ProjectDTO)");

    SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryProject();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {/*from   w ww.  j a  va 2s  .c om*/

        // insert project
        ProjectWithBLOBs lProjectWithBLOBs = new ProjectWithBLOBs();
        lProjectWithBLOBs.setIdProject(pProjectDTO.getIdProject());
        lProjectWithBLOBs.setName(pProjectDTO.getName());
        lProjectWithBLOBs.setLanguage(pProjectDTO.getLanguage());
        lProjectWithBLOBs.setBibiscoVersion(pProjectDTO.getBibiscoVersion());

        ProjectMapper lProjectMapper = lSqlSession.getMapper(ProjectMapper.class);
        lProjectMapper.insertSelective(lProjectWithBLOBs);

        lSqlSession.commit();

    } catch (Throwable t) {
        mLog.error(t);
        lSqlSession.rollback();
        throw new BibiscoException(t, BibiscoException.SQL_EXCEPTION);
    } finally {
        lSqlSession.close();
    }

    mLog.debug("End insertIntoBibiscoDB(ProjectDTO)");
}

From source file:com.bibisco.manager.ProjectManager.java

License:GNU General Public License

public static void delete(String pStrIdProject) {

    mLog.debug("Start delete(String)");

    // validate preconditions
    Validate.notEmpty(pStrIdProject, "Id project cannot be empty");
    Validate.isTrue(ProjectManager.projectExists(pStrIdProject), "Project references non existent directory");

    SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryBibisco();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {// w  ww .  j a  va2s .  c  o  m

        // delete entry on bibisco db
        ProjectsMapper lProjectsMapper = lSqlSession.getMapper(ProjectsMapper.class);
        lProjectsMapper.deleteByPrimaryKey(pStrIdProject);

        // delete file
        FileUtils.deleteDirectory(new File(getDBProjectDirectory(pStrIdProject)));

        lSqlSession.commit();

    } catch (Throwable t) {
        mLog.error(t);
        lSqlSession.rollback();
        throw new BibiscoException(t, BibiscoException.SQL_EXCEPTION);
    } finally {
        lSqlSession.close();
    }

    mLog.debug("End delete(String)");
}

From source file:com.bibisco.manager.ProjectManager.java

License:GNU General Public License

private static void insertIntoBibiscoDB(ProjectDTO pProjectDTO) {

    mLog.debug("Start insertIntoBibiscoDB(ProjectDTO)");

    SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryBibisco();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {/*from w ww.  j  a  va  2 s . c o m*/
        Projects lProjects = new Projects();
        lProjects.setIdProject(pProjectDTO.getIdProject());
        lProjects.setName(pProjectDTO.getName());

        ProjectsMapper lProjectsMapper = lSqlSession.getMapper(ProjectsMapper.class);
        lProjectsMapper.insertSelective(lProjects);

        lSqlSession.commit();

    } catch (Throwable t) {
        mLog.error(t);
        lSqlSession.rollback();
        throw new BibiscoException(t, BibiscoException.SQL_EXCEPTION);
    } finally {
        lSqlSession.close();
    }

    mLog.debug("End insertIntoBibiscoDB(ProjectDTO)");
}

From source file:com.bibisco.manager.ProjectManager.java

License:GNU General Public License

public static void save(ProjectDTO pProjectDTO) {

    mLog.debug("Start save(ProjectDTO)");

    // validate preconditions
    Validate.notNull(pProjectDTO, "argument ProjectDTO cannot be null");

    // open session in bibisco db
    SqlSessionFactory lSqlSessionFactoryBibisco = SqlSessionFactoryManager.getInstance()
            .getSqlSessionFactoryBibisco();
    SqlSession lSqlSessionBibisco = lSqlSessionFactoryBibisco.openSession();

    // open session in project db
    SqlSessionFactory lSqlSessionFactoryProject = SqlSessionFactoryManager.getInstance()
            .getSqlSessionFactoryProject();
    SqlSession lSqlSessionProject = lSqlSessionFactoryProject.openSession();

    try {//from ww  w  . j  a va 2 s  . co  m

        Projects lProjects = new Projects();
        lProjects.setIdProject(ContextManager.getInstance().getIdProject());
        lProjects.setName(pProjectDTO.getName());

        ProjectsMapper lProjectsMapper = lSqlSessionBibisco.getMapper(ProjectsMapper.class);
        lProjectsMapper.updateByPrimaryKey(lProjects);

        ProjectWithBLOBs lProjectWithBLOBs = new ProjectWithBLOBs();
        lProjectWithBLOBs.setIdProject(ContextManager.getInstance().getIdProject());
        lProjectWithBLOBs.setName(pProjectDTO.getName());

        ProjectMapper lProjectMapper = lSqlSessionProject.getMapper(ProjectMapper.class);
        lProjectMapper.updateByPrimaryKeySelective(lProjectWithBLOBs);

        lSqlSessionBibisco.commit();
        lSqlSessionProject.commit();

    } catch (Throwable t) {
        mLog.error(t);
        lSqlSessionBibisco.rollback();
        lSqlSessionProject.rollback();
        throw new BibiscoException(t, BibiscoException.SQL_EXCEPTION);
    } finally {
        lSqlSessionBibisco.close();
        lSqlSessionProject.close();
    }

    mLog.debug("End save(ProjectDTO)");
}