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

License:GNU General Public License

public static SceneDTO insert(SceneDTO pSceneDTO) {

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

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

        // Insert scene
        Scenes lScenes = new Scenes();
        lScenes.setIdChapter(pSceneDTO.getIdChapter());
        lScenes.setDescription(pSceneDTO.getDescription());
        lScenes.setPosition(pSceneDTO.getPosition());

        ScenesMapper lScenesMapper = lSqlSession.getMapper(ScenesMapper.class);
        lScenesMapper.insertSelective(lScenes);

        pSceneDTO.setIdScene(lScenes.getIdScene().intValue());
        pSceneDTO.setTaskStatus(TaskStatus.TODO);

        // insert scene revision
        SceneRevisions lSceneRevisions = new SceneRevisions();
        lSceneRevisions.setIdScene(lScenes.getIdScene().intValue());
        lSceneRevisions.setRevisionNumber(1);
        lSceneRevisions.setSelected("Y");
        lSceneRevisions.setWords(0);
        lSceneRevisions.setCharacters(0);

        SceneRevisionsMapper lSceneRevisionsMapper = lSqlSession.getMapper(SceneRevisionsMapper.class);
        lSceneRevisionsMapper.insert(lSceneRevisions);

        lSqlSession.commit();

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

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

    return pSceneDTO;
}

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

License:GNU General Public License

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

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

    SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryProject();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();

    try {//from   w  w  w  . j  ava  2 s . c o  m

        // get scene to update
        ScenesMapper lScenesMapper = lSqlSession.getMapper(ScenesMapper.class);
        Scenes lScenes = lScenesMapper.selectByPrimaryKey(pIntIdScene.longValue());
        Integer pIntSourcePosition = lScenes.getPosition();

        if (pIntSourcePosition.intValue() != pIntDestPosition.intValue()) {

            // update scene position with fake position to preserve unique index before shift
            lScenes.setPosition(-1);
            lScenesMapper.updateByPrimaryKey(lScenes);

            // update other scenes' position
            Integer lIntStartPosition;
            Integer lIntEndPosition;
            if (pIntSourcePosition > pIntDestPosition) {
                lIntStartPosition = pIntDestPosition;
                lIntEndPosition = pIntSourcePosition;
                lScenesMapper.shiftUp(lIntStartPosition, lIntEndPosition, lScenes.getIdChapter());
            } else {
                lIntStartPosition = pIntSourcePosition;
                lIntEndPosition = pIntDestPosition;
                lScenesMapper.shiftDown(lIntStartPosition, lIntEndPosition, lScenes.getIdChapter());
            }

            // update scene position
            lScenes.setPosition(pIntDestPosition);
            lScenesMapper.updateByPrimaryKey(lScenes);

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

License:GNU General Public License

public static void save(SceneDTO pSceneDTO) {

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

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

        Scenes lScenes = new Scenes();
        lScenes.setIdScene(pSceneDTO.getIdScene().longValue());
        lScenes.setPosition(pSceneDTO.getPosition());
        lScenes.setDescription(pSceneDTO.getDescription());
        lScenes.setIdChapter(pSceneDTO.getIdChapter());
        lScenes.setTaskStatus(pSceneDTO.getTaskStatus() != null ? pSceneDTO.getTaskStatus().getValue() : null);

        ScenesMapper lScenesMapper = lSqlSession.getMapper(ScenesMapper.class);
        lScenesMapper.updateByPrimaryKeySelective(lScenes);

        lSqlSession.commit();

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

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

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

License:GNU General Public License

public static void save(SceneRevisionDTO pSceneRevisionDTO) {

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

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

        Scenes lScenes = new Scenes();
        lScenes.setIdScene(pSceneRevisionDTO.getIdScene().longValue());
        lScenes.setPosition(pSceneRevisionDTO.getPosition());
        lScenes.setTaskStatus(
                pSceneRevisionDTO.getTaskStatus() != null ? pSceneRevisionDTO.getTaskStatus().getValue()
                        : null);

        ScenesMapper lScenesMapper = lSqlSession.getMapper(ScenesMapper.class);
        lScenesMapper.updateByPrimaryKeySelective(lScenes);

        SceneRevisions lSceneRevisions = new SceneRevisions();
        lSceneRevisions.setIdScene(pSceneRevisionDTO.getIdScene());
        lSceneRevisions.setIdSceneRevision(pSceneRevisionDTO.getIdRevision().longValue());
        lSceneRevisions.setRevisionNumber(pSceneRevisionDTO.getRevision());
        lSceneRevisions.setSelected("Y");
        lSceneRevisions.setIdLocation(pSceneRevisionDTO.getIdLocation());
        lSceneRevisions.setPointOfView(
                pSceneRevisionDTO.getPointOfView() != null ? pSceneRevisionDTO.getPointOfView().getValue()
                        : null);
        lSceneRevisions.setPointOfViewIdCharacter(pSceneRevisionDTO.getIdCharacterPointOfView());
        lSceneRevisions.setScene(pSceneRevisionDTO.getText());
        lSceneRevisions.setSceneDate(pSceneRevisionDTO.getSceneDate());
        lSceneRevisions.setWords(pSceneRevisionDTO.getWordCount());
        lSceneRevisions.setCharacters(pSceneRevisionDTO.getCharacterCount());
        SceneRevisionsMapper lSceneRevisionsMapper = lSqlSession.getMapper(SceneRevisionsMapper.class);
        lSceneRevisionsMapper.updateByPrimaryKeyWithBLOBs(lSceneRevisions);

        // save characters
        SceneRevisionCharactersMapper lSceneRevisionCharactersMapper = lSqlSession
                .getMapper(SceneRevisionCharactersMapper.class);
        SceneRevisionCharactersExample lSceneRevisionCharactersExample = new SceneRevisionCharactersExample();
        lSceneRevisionCharactersExample.createCriteria()
                .andIdSceneRevisionEqualTo((pSceneRevisionDTO.getIdRevision()));
        lSceneRevisionCharactersMapper.deleteByExample(lSceneRevisionCharactersExample);

        if (pSceneRevisionDTO.getCharacters() != null && pSceneRevisionDTO.getCharacters().size() > 0) {
            for (Integer lIntIdCharacter : pSceneRevisionDTO.getCharacters()) {
                SceneRevisionCharactersKey lSceneRevisionCharactersKey = new SceneRevisionCharactersKey();
                lSceneRevisionCharactersKey.setIdCharacter(lIntIdCharacter);
                lSceneRevisionCharactersKey.setIdSceneRevision(pSceneRevisionDTO.getIdRevision());
                lSceneRevisionCharactersMapper.insert(lSceneRevisionCharactersKey);
            }
        }

        // save strands
        SceneRevisionStrandsMapper lSceneRevisionStrandsMapper = lSqlSession
                .getMapper(SceneRevisionStrandsMapper.class);
        SceneRevisionStrandsExample lSceneRevisionStrandsExample = new SceneRevisionStrandsExample();
        lSceneRevisionStrandsExample.createCriteria()
                .andIdSceneRevisionEqualTo((pSceneRevisionDTO.getIdRevision()));
        lSceneRevisionStrandsMapper.deleteByExample(lSceneRevisionStrandsExample);

        if (pSceneRevisionDTO.getStrands() != null && pSceneRevisionDTO.getStrands().size() > 0) {
            for (Integer lIntIdStrand : pSceneRevisionDTO.getStrands()) {
                SceneRevisionStrandsKey lSceneRevisionStrands = new SceneRevisionStrandsKey();
                lSceneRevisionStrands.setIdStrand(lIntIdStrand);
                lSceneRevisionStrands.setIdSceneRevision(pSceneRevisionDTO.getIdRevision());
                lSceneRevisionStrandsMapper.insert(lSceneRevisionStrands);
            }
        }

        lSqlSession.commit();

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

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

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

License:GNU General Public License

public static StrandDTO insert(StrandDTO pStrandDTO) {

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

    SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryProject();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {// w  w w .  ja  v a  2s .  c  o m
        Strands lStrands = new Strands();
        lStrands.setName(pStrandDTO.getName());
        lStrands.setPosition(pStrandDTO.getPosition());

        StrandsMapper lStrandsMapper = lSqlSession.getMapper(StrandsMapper.class);
        lStrandsMapper.insertSelective(lStrands);

        pStrandDTO.setIdStrand(lStrands.getIdStrand().intValue());
        pStrandDTO.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(StrandDTO)");

    return pStrandDTO;
}

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

License:GNU General Public License

public static boolean deleteByPosition(Integer pIntPosition) {

    boolean lBlnResult;

    mLog.debug("Start deleteByPosition(" + pIntPosition + ")");

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

        StrandsMapper lStrandsMapper = lSqlSession.getMapper(StrandsMapper.class);

        // get strand by position
        StrandsExample lStrandsExample = new StrandsExample();
        lStrandsExample.createCriteria().andPositionEqualTo(pIntPosition);
        Strands lStrands = lStrandsMapper.selectByExample(lStrandsExample).get(0);

        // check if strand is referenced
        boolean lBlnIsReferenced = checkIfReferenced(lStrands.getIdStrand().intValue(), lSqlSession);

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

            // delete strand
            lStrandsMapper.deleteByPrimaryKey(lStrands.getIdStrand());

            // shift down other strand
            lStrandsMapper.shiftDown(pIntPosition, Integer.MAX_VALUE);

            lBlnResult = true;
        }
        // strand 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 deleteStrandByPosition(" + pIntPosition + ")");

    return lBlnResult;
}

From source file:com.bibisco.manager.StrandManager.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  ww  .  j a  v a2 s .  c  o  m

            StrandsExample lStrandsExample = new StrandsExample();
            lStrandsExample.createCriteria().andPositionEqualTo(pIntSourcePosition);

            StrandsMapper lStrandsMapper = lSqlSession.getMapper(StrandsMapper.class);

            // get strand to update
            Strands lStrands = lStrandsMapper.selectByExample(lStrandsExample).get(0);

            // update strand position with fake position to preserve unique index before shift
            lStrands.setPosition(-1);
            lStrandsMapper.updateByPrimaryKey(lStrands);

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

            // update strand position
            lStrands.setPosition(pIntDestPosition);
            lStrandsMapper.updateByPrimaryKey(lStrands);

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

License:GNU General Public License

public static void save(StrandDTO pStrandDTO) {

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

    SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryProject();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {/*from  ww w .  ja v a2 s. c o  m*/
        Strands lStrands = new Strands();
        lStrands.setIdStrand(pStrandDTO.getIdStrand().longValue());
        lStrands.setName(pStrandDTO.getName());
        lStrands.setDescription(pStrandDTO.getDescription());
        if (pStrandDTO.getTaskStatus() != null) {
            lStrands.setTaskStatus(pStrandDTO.getTaskStatus().getValue());
        }

        StrandsMapper lStrandsMapper = lSqlSession.getMapper(StrandsMapper.class);
        lStrandsMapper.updateByPrimaryKeySelective(lStrands);

        lSqlSession.commit();

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

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

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

License:GNU General Public License

private static boolean getDonationTip(Properties lProperties) {

    if (lProperties.getValue() == "false") {
        return false;
    }/* w  ww  . ja v  a2s. c  o m*/

    // it's first time...
    else if (StringUtils.isEmpty(lProperties.getValue())) {
        String lStrDate = mDateFormat.format(new Date());
        SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance()
                .getSqlSessionFactoryBibisco();
        SqlSession lSqlSession = lSqlSessionFactory.openSession();
        try {
            PropertiesMapper lPropertiesMapper = lSqlSession.getMapper(PropertiesMapper.class);
            Properties lPropertiesToInsert = new Properties();
            lPropertiesToInsert.setProperty("donationTip");
            lPropertiesToInsert.setValue(lStrDate);
            lPropertiesMapper.updateByPrimaryKey(lPropertiesToInsert);
            lSqlSession.commit();
        } catch (Throwable t) {
            mLog.error(t);
            lSqlSession.rollback();
            throw new BibiscoException(t, BibiscoException.SQL_EXCEPTION);
        } finally {
            lSqlSession.close();
        }
        return false;
    }

    else {
        try {
            Date lDate = mDateFormat.parse(lProperties.getValue());
            Date lDateNow = new Date();
            if (lDateNow.after(DateUtils.addDays(lDate, 30))) {
                return true;
            } else {
                return false;
            }
        } catch (ParseException e) {
            mLog.error(e);
            return false;
        }
    }
}

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

License:GNU General Public License

public static void disableTip(String pStrTipCode) {

    mLog.debug("Start disableTip(" + pStrTipCode + ")");

    SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryBibisco();
    SqlSession lSqlSession = lSqlSessionFactory.openSession();
    try {//from   ww w .ja v a  2s.  c o m
        PropertiesMapper lPropertiesMapper = lSqlSession.getMapper(PropertiesMapper.class);

        Properties lProperties = new Properties();
        lProperties.setProperty(pStrTipCode);
        lProperties.setValue("false");
        lPropertiesMapper.updateByPrimaryKey(lProperties);

        lSqlSession.commit();

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

    mLog.debug("End disableTip(" + pStrTipCode + ")");
}