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.ChapterManager.java

License:GNU General Public License

public static List<ChapterDTO> loadAll() {

    List<ChapterDTO> lListChapter = null;

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

    SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryProject();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {/*from w  w  w. j a v  a2s . c  o m*/

        VChaptersMapper lVChaptersMapper = lSqlSession.getMapper(VChaptersMapper.class);
        VChaptersExample lVChaptersExample = new VChaptersExample();
        lVChaptersExample.setOrderByClause("position");
        List<VChapters> lListVChapters = lVChaptersMapper.selectByExample(lVChaptersExample);

        if (lListVChapters != null && lListVChapters.size() > 0) {
            lListChapter = new ArrayList<ChapterDTO>();
            for (VChapters lVChapters : lListVChapters) {

                ChapterDTO lChapterDTO = new ChapterDTO();
                lChapterDTO.setIdChapter(lVChapters.getIdChapter().intValue());
                lChapterDTO.setPosition(lVChapters.getPosition());
                lChapterDTO.setTitle(lVChapters.getTitle());
                lChapterDTO.setTaskStatus(calculateChapterTaskStatus(lVChapters));
                lChapterDTO.setReasonTaskStatus(
                        TaskStatus.getTaskStatusFromValue(lVChapters.getReasonTaskStatus()));
                lChapterDTO.setWordCount(lVChapters.getWords());
                lChapterDTO.setCharacterCount(lVChapters.getCharacters());

                lListChapter.add(lChapterDTO);
            }
        }

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

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

    return lListChapter;
}

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

License:GNU General Public License

public static void save(ChapterDTO pChapterDTO) {

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

    SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryProject();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {/*  w ww  . ja v  a  2 s.c o m*/

        ChaptersWithBLOBs lChapters = new ChaptersWithBLOBs();
        lChapters.setIdChapter(pChapterDTO.getIdChapter().longValue());
        lChapters.setPosition(pChapterDTO.getPosition());
        lChapters.setTitle(pChapterDTO.getTitle());
        lChapters.setReasonTaskStatus(
                pChapterDTO.getReasonTaskStatus() != null ? pChapterDTO.getReasonTaskStatus().getValue()
                        : null);
        lChapters.setReason(pChapterDTO.getReason());
        lChapters.setNote(pChapterDTO.getNote());

        ChaptersMapper lChaptersMapper = lSqlSession.getMapper(ChaptersMapper.class);
        lChaptersMapper.updateByPrimaryKeySelective(lChapters);

        lSqlSession.commit();

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

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

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

License:GNU General Public License

public static void deleteByPosition(Integer pIntIdPosition) {

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

    SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryProject();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {//from w ww.ja  va  2  s .c  o m
        // delete scene revisions
        SceneRevisionsMapper lSceneRevisionsMapper = lSqlSession.getMapper(SceneRevisionsMapper.class);
        lSceneRevisionsMapper.deleteByChapterPosition(pIntIdPosition);

        // delete scenes
        ScenesMapper lScenesMapper = lSqlSession.getMapper(ScenesMapper.class);
        lScenesMapper.deleteByChapterPosition(pIntIdPosition);

        // delete chapter
        ChaptersMapper lChaptersMapper = lSqlSession.getMapper(ChaptersMapper.class);
        lChaptersMapper.deleteByPosition(pIntIdPosition);

        // shift down other chapters
        lChaptersMapper.shiftDown(pIntIdPosition, Integer.MAX_VALUE);

        lSqlSession.commit();

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

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

}

From source file:com.bibisco.manager.ChapterManager.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 .ja  va  2 s  .  c o  m

            ChaptersExample lChaptersExample = new ChaptersExample();
            lChaptersExample.createCriteria().andPositionEqualTo(pIntSourcePosition);

            ChaptersMapper lChaptersMapper = lSqlSession.getMapper(ChaptersMapper.class);

            // get chapter to update
            Chapters lChapters = lChaptersMapper.selectByExample(lChaptersExample).get(0);

            // update chapter position with fake position to preserve unique index before shift
            lChapters.setPosition(-1);
            lChaptersMapper.updateByPrimaryKey(lChapters);

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

            // update chapter position
            lChapters.setPosition(pIntDestPosition);
            lChaptersMapper.updateByPrimaryKey(lChapters);

            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.CharacterManager.java

License:GNU General Public License

private static List<CharacterDTO> loadCharacters(Boolean pBlnMain) {

    List<CharacterDTO> lCharacterDTOList = null;

    mLog.debug("Start loadMainCharacters(" + pBlnMain + ")");

    SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryProject();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {//from  w  ww .  j a  v a2 s.  c o m

        // create select criteria
        CharactersMapper lCharactersMapper = lSqlSession.getMapper(CharactersMapper.class);
        CharactersExample lCharactersExample = new CharactersExample();
        if (pBlnMain != null && pBlnMain.booleanValue()) {
            lCharactersExample.createCriteria().andMainCharacterEqualTo("Y");
        } else if (pBlnMain != null && !pBlnMain.booleanValue()) {
            lCharactersExample.createCriteria().andMainCharacterEqualTo("N");
        }
        lCharactersExample.setOrderByClause("POSITION");

        List<Characters> lCharactersList = lCharactersMapper.selectByExample(lCharactersExample);
        if (lCharactersList != null && lCharactersList.size() > 0) {
            lCharacterDTOList = new ArrayList<CharacterDTO>();
            for (Characters lCharacters : lCharactersList) {

                // create CharacterDTO
                CharacterDTO lCharacterDTO;
                if (pBlnMain) {
                    lCharacterDTO = createMainCharacterDTOFromCharacters(lCharacters);
                } else {
                    lCharacterDTO = createSecondaryCharacterDTOFromCharacters(lCharacters);
                }

                // add character to list
                lCharacterDTOList.add(lCharacterDTO);
            }
        }

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

    mLog.debug("End loadMainCharacters(" + pBlnMain + ")");

    return lCharacterDTOList;

}

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

License:GNU General Public License

public static CharacterDTO insert(CharacterDTO pCharacterDTO) {

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

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

        CharactersWithBLOBs lCharacters = new CharactersWithBLOBs();
        lCharacters.setMainCharacter(pCharacterDTO.isMainCharacter() ? "Y" : "N");
        lCharacters.setName(pCharacterDTO.getName());
        lCharacters.setPosition(pCharacterDTO.getPosition());

        CharactersMapper lCharactersMapper = lSqlSession.getMapper(CharactersMapper.class);
        lCharactersMapper.insertSelective(lCharacters);

        pCharacterDTO = createCharacterDTOFromCharacters(lCharacters);
        pCharacterDTO.setTaskStatus(TaskStatus.TODO);

        if (pCharacterDTO.isMainCharacter()) {
            // populate character info
            CharacterInfosMapper lCharacterInfosMapper = lSqlSession.getMapper(CharacterInfosMapper.class);
            for (CharacterInfoQuestions lCharacterInfoQuestions : CharacterInfoQuestions.values()) {
                for (int i = 0; i < lCharacterInfoQuestions.getTotalQuestions(); i++) {
                    CharacterInfos lCharacterInfos = new CharacterInfos();
                    lCharacterInfos.setCharacterInfoType(lCharacterInfoQuestions.name());
                    lCharacterInfos.setIdCharacter(lCharacters.getIdCharacter().intValue());
                    lCharacterInfos.setQuestion(i + 1);
                    lCharacterInfosMapper.insert(lCharacterInfos);
                }
            }
        }

        lSqlSession.commit();

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

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

    return pCharacterDTO;
}

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

License:GNU General Public License

public static MainCharacterDTO loadMainCharacter(Integer lIntIdCharacter) {

    MainCharacterDTO lMainCharacterDTO = null;

    mLog.debug("Start loadMainCharacter(Integer)");
    SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryProject();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {//  ww  w.  j  a  v  a  2  s .c o m

        CharactersMapper lCharactersMapper = lSqlSession.getMapper(CharactersMapper.class);
        CharactersWithBLOBs lCharacters = lCharactersMapper.selectByPrimaryKey(lIntIdCharacter.longValue());

        lMainCharacterDTO = createMainCharacterDTOFromCharacters(lCharacters);

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

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

    return lMainCharacterDTO;

}

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

License:GNU General Public License

private static boolean deleteCharacterByPosition(Integer pIntPosition, boolean pBlnMainCharacter) {

    boolean lBlnResult;

    mLog.debug("Start deleteCharacterByPosition(" + pIntPosition + "," + pBlnMainCharacter + ")");

    // get character by position
    SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryProject();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {/*from w  w  w .j  a  v  a2  s .c  om*/

        CharactersMapper lCharactersMapper = lSqlSession.getMapper(CharactersMapper.class);

        //get character by position and main flag
        CharactersExample lCharactersExample = new CharactersExample();
        lCharactersExample.createCriteria().andPositionEqualTo(pIntPosition)
                .andMainCharacterEqualTo(pBlnMainCharacter ? "Y" : "N");
        Characters lCharacters = lCharactersMapper.selectByExample(lCharactersExample).get(0);

        // check if character is referenced
        boolean lBlnIsReferenced = checkIfReferenced(lCharacters.getIdCharacter().intValue(), lSqlSession);

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

            // delete character infos
            deleteCharacterInfosByIdCharacter(lCharacters.getIdCharacter().intValue(), lSqlSession);

            // delete character
            lCharactersMapper.deleteByPrimaryKey(lCharacters.getIdCharacter());

            // shift down other character
            lCharactersMapper.shiftDown(pIntPosition, Integer.MAX_VALUE, pBlnMainCharacter ? "Y" : "N");

            // delete images
            ImageManager.deleteImagesByElement(lSqlSession, lCharacters.getIdCharacter().intValue(),
                    ElementType.CHARACTERS);

            lBlnResult = true;
        }
        // character 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();
    }

    mLog.debug("End deleteCharacterByPosition(" + pIntPosition + "," + pBlnMainCharacter + ")");

    return lBlnResult;
}

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

License:GNU General Public License

private static void move(Integer pIntSourcePosition, Integer pIntDestPosition, boolean pBlnMainCharacter) {

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

    if (!pIntSourcePosition.equals(pIntDestPosition)) {
        SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance()
                .getSqlSessionFactoryProject();
        SqlSession lSqlSession = lSqlSessionFactory.openSession();
        try {/*  ww w .j av  a2s.  c o m*/

            CharactersMapper lCharactersMapper = lSqlSession.getMapper(CharactersMapper.class);

            //get character by position and main flag
            CharactersExample lCharactersExample = new CharactersExample();
            lCharactersExample.createCriteria().andPositionEqualTo(pIntSourcePosition)
                    .andMainCharacterEqualTo(pBlnMainCharacter ? "Y" : "N");
            Characters lCharacters = lCharactersMapper.selectByExample(lCharactersExample).get(0);

            // update character position with fake position to preserve unique index before shift
            lCharacters.setPosition(-1);
            lCharactersMapper.updateByPrimaryKey(lCharacters);

            // update other Characters' position
            Integer lIntStartPosition;
            Integer lIntEndPosition;
            if (pIntSourcePosition > pIntDestPosition) {
                lIntStartPosition = pIntDestPosition;
                lIntEndPosition = pIntSourcePosition;
                lCharactersMapper.shiftUp(lIntStartPosition, lIntEndPosition, pBlnMainCharacter ? "Y" : "N");
            } else {
                lIntStartPosition = pIntSourcePosition;
                lIntEndPosition = pIntDestPosition;
                lCharactersMapper.shiftDown(lIntStartPosition, lIntEndPosition, pBlnMainCharacter ? "Y" : "N");
            }

            // update character position
            lCharacters.setPosition(pIntDestPosition);
            lCharactersMapper.updateByPrimaryKey(lCharacters);

            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, boolean)");
}

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

License:GNU General Public License

public static CharacterInfoQuestionsDTO loadCharacterInfoQuestions(
        CharacterInfoQuestions pCharacterInfoQuestions, Integer lIntIdCharacter) {

    CharacterInfoQuestionsDTO lCharacterInfoQuestionsDTO;

    mLog.debug("Start loadCharacterInfo(CharacterInfoQuestions, Integer)");

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

        CharactersMapper lCharactersMapper = lSqlSession.getMapper(CharactersMapper.class);
        CharactersWithBLOBs lCharacters = lCharactersMapper.selectByPrimaryKey(lIntIdCharacter.longValue());

        lCharacterInfoQuestionsDTO = new CharacterInfoQuestionsDTO();
        lCharacterInfoQuestionsDTO.setId(lIntIdCharacter);
        lCharacterInfoQuestionsDTO.setCharacterInfoQuestions(pCharacterInfoQuestions);

        switch (pCharacterInfoQuestions) {

        case BEHAVIORS:
            lCharacterInfoQuestionsDTO.setFreeText(lCharacters.getBehaviorsFreeText());
            lCharacterInfoQuestionsDTO
                    .setTaskStatus(TaskStatus.getTaskStatusFromValue(lCharacters.getBehaviorsTaskStatus()));
            lCharacterInfoQuestionsDTO
                    .setInterviewMode(lCharacters.getBehaviorsInterview().equals("Y") ? true : false);
            break;

        case IDEAS:
            lCharacterInfoQuestionsDTO.setFreeText(lCharacters.getIdeasFreeText());
            lCharacterInfoQuestionsDTO
                    .setTaskStatus(TaskStatus.getTaskStatusFromValue(lCharacters.getIdeasTaskStatus()));
            lCharacterInfoQuestionsDTO
                    .setInterviewMode(lCharacters.getIdeasInterview().equals("Y") ? true : false);
            break;

        case PERSONAL_DATA:
            lCharacterInfoQuestionsDTO.setFreeText(lCharacters.getPersonalDataFreeText());
            lCharacterInfoQuestionsDTO
                    .setTaskStatus(TaskStatus.getTaskStatusFromValue(lCharacters.getPersonalDataTaskStatus()));
            lCharacterInfoQuestionsDTO
                    .setInterviewMode(lCharacters.getPersonalDataInterview().equals("Y") ? true : false);
            break;

        case PHYSIONOMY:
            lCharacterInfoQuestionsDTO.setFreeText(lCharacters.getPhysionomyFreeText());
            lCharacterInfoQuestionsDTO
                    .setTaskStatus(TaskStatus.getTaskStatusFromValue(lCharacters.getPhysionomyTaskStatus()));
            lCharacterInfoQuestionsDTO
                    .setInterviewMode(lCharacters.getPhysionomyInterview().equals("Y") ? true : false);
            break;

        case PSYCHOLOGY:
            lCharacterInfoQuestionsDTO.setFreeText(lCharacters.getPsychologyFreeText());
            lCharacterInfoQuestionsDTO
                    .setTaskStatus(TaskStatus.getTaskStatusFromValue(lCharacters.getPsychologyTaskStatus()));
            lCharacterInfoQuestionsDTO
                    .setInterviewMode(lCharacters.getPsychologyInterview().equals("Y") ? true : false);
            break;

        case SOCIOLOGY:
            lCharacterInfoQuestionsDTO.setFreeText(lCharacters.getSociologyFreeText());
            lCharacterInfoQuestionsDTO
                    .setTaskStatus(TaskStatus.getTaskStatusFromValue(lCharacters.getSociologyTaskStatus()));
            lCharacterInfoQuestionsDTO
                    .setInterviewMode(lCharacters.getSociologyInterview().equals("Y") ? true : false);
            break;
        default:
            break;
        }

        // get answers
        CharacterInfosExample lCharacterInfosExample = new CharacterInfosExample();
        lCharacterInfosExample.createCriteria().andCharacterInfoTypeEqualTo(pCharacterInfoQuestions.name())
                .andIdCharacterEqualTo(lCharacters.getIdCharacter().intValue());
        lCharacterInfosExample.setOrderByClause("question");

        CharacterInfosMapper lCharacterInfosMapper = lSqlSession.getMapper(CharacterInfosMapper.class);
        List<CharacterInfos> lListCharacterInfos = lCharacterInfosMapper
                .selectByExampleWithBLOBs(lCharacterInfosExample);

        List<String> lListAnswers = new ArrayList<String>();
        for (CharacterInfos lCharacterInfos : lListCharacterInfos) {
            lListAnswers.add(StringUtils.defaultString(lCharacterInfos.getInfo()));
        }
        lCharacterInfoQuestionsDTO.setAnswerList(lListAnswers);

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

    mLog.debug("End loadCharacterInfo(CharacterInfoQuestions, Integer)");

    return lCharacterInfoQuestionsDTO;
}