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

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

Introduction

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

Prototype

@Override
void close();

Source Link

Document

Closes the session.

Usage

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

License:GNU General Public License

public static void delete(Integer pIntIdImage) {

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

    // validate preconditions
    Validate.notNull(pIntIdImage, "argument idImage cannot be null");

    SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryProject();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {//from w w  w .  j  a  v a2s.  c om
        ImagesMapper lImagesMapper = lSqlSession.getMapper(ImagesMapper.class);

        // load image to delete
        Images lImages = lImagesMapper.selectByPrimaryKey(pIntIdImage.longValue());

        if (lImages != null) {
            // delete image file on disk
            deleteFile(lImages.getFileName());

            // delete image on db
            lImagesMapper.deleteByPrimaryKey(pIntIdImage.longValue());

            lSqlSession.commit();
        }

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

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

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

License:GNU General Public License

private Locale initLocale() {

    Locale lLocale = null;//from   w  w w  . j a  v a 2s.  c  o  m

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

    SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryBibisco();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {

        PropertiesMapper lPropertiesMapper = lSqlSession.getMapper(PropertiesMapper.class);
        Properties lProperties = lPropertiesMapper.selectByPrimaryKey("locale");

        if (lProperties != null) {
            String[] lStrLocaleSplit = lProperties.getValue().split("_");
            lLocale = new Locale(lStrLocaleSplit[0], lStrLocaleSplit[1]);

        } else {
            lLocale = Locale.getDefault();

            // save locale
            lProperties = new Properties();
            lProperties.setProperty("locale");
            lProperties.setValue(lLocale.toString());
            lPropertiesMapper.insert(lProperties);

            lSqlSession.commit();
        }

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

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

    return lLocale;
}

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 www .  jav  a  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 List<LocationDTO> loadAll() {

    List<LocationDTO> lListLocationDTO = null;

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

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

        LocationsMapper lLocationsMapper = lSqlSession.getMapper(LocationsMapper.class);
        LocationsExample lLocationsExample = new LocationsExample();
        lLocationsExample.setOrderByClause("position");
        List<Locations> lListLocations = lLocationsMapper.selectByExample(lLocationsExample);

        if (lListLocations != null && lListLocations.size() > 0) {
            lListLocationDTO = new ArrayList<LocationDTO>();
            for (Locations lLocations : lListLocations) {

                LocationDTO lLocationDTO = new LocationDTO();
                lLocationDTO.setIdLocation(lLocations.getIdLocation().intValue());
                lLocationDTO.setName(lLocations.getName());
                lLocationDTO.setPosition(lLocations.getPosition());
                lLocationDTO.setNation(lLocations.getNation());
                lLocationDTO.setState(lLocations.getState());
                lLocationDTO.setCity(lLocations.getCity());
                lLocationDTO.setTaskStatus(TaskStatus.getTaskStatusFromValue(lLocations.getTaskStatus()));

                lListLocationDTO.add(lLocationDTO);
            }
        }

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

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

    return lListLocationDTO;
}

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 {/*  w ww .  java2  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  ava  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 ww  w.j  a v a2 s .c  o 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 LocationDTO load(Integer pIntIdLocation) {

    LocationDTO lLocationDTO = null;/*from w w w  . ja  v  a2s  .  com*/

    mLog.debug("Start load(Integer)");
    SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryProject();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {

        LocationsMapper lLocationsMapper = lSqlSession.getMapper(LocationsMapper.class);
        Locations lLocations = lLocationsMapper.selectByPrimaryKey(pIntIdLocation.longValue());
        lLocationDTO = createLocationDTOFromLocations(lLocations);

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

    mLog.debug("End load(Integer)");

    return lLocationDTO;
}

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 {/*from   w  w  w  .  ja  va  2s.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.ProjectFromSceneManager.java

License:GNU General Public License

public static ProjectFromSceneChapterDTO loadChapterByIdScene(Integer pIntIdScene) {

    ProjectFromSceneChapterDTO lProjectFromSceneChapterDTO;

    mLog.debug("Start loadChapterByIdScene(" + pIntIdScene + ")");

    //load scene//w  ww  . ja  v a 2 s.c o  m
    Scenes lScenes;
    SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryProject();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {
        ScenesMapper lScenesMapper = lSqlSession.getMapper(ScenesMapper.class);
        lScenes = lScenesMapper.selectByPrimaryKey(pIntIdScene.longValue());
    } catch (Throwable t) {
        mLog.error(t);
        throw new BibiscoException(t, BibiscoException.SQL_EXCEPTION);
    } finally {
        lSqlSession.close();
    }

    lProjectFromSceneChapterDTO = loadChapterByIdChapter(lScenes.getIdChapter());
    mLog.debug("End loadChapterByIdScene(" + pIntIdScene + ")");

    return lProjectFromSceneChapterDTO;
}