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:in.flipbrain.dao.MyBatisDao.java

License:Apache License

public int saveTrail(TrailDto trail) {
    int rows = 0;
    SqlSession session = sqlSessionFactory.openSession();
    try {//from   w w w .j av a  2  s. c o m
        if (trail.trailId < 1 && !trail.isDeleted()) {
            rows += session.insert("insertTrail", trail);
        } else if (trail.isDeleted()) {
            rows += session.delete("deleteTrail", trail.trailId);
        } else {
            rows += session.delete("updateTrail", trail);
        }
        ArrayList<TrailItemDto> items = new ArrayList<TrailItemDto>();
        items.addAll(trail.resources);
        items.addAll(trail.videos);
        int seq = 0;
        for (TrailItemDto t : items) {
            t.trailId = trail.trailId;
            if (t.itemId < 1 && !t.isDeleted()) {
                t.seqNo = seq++;
                rows += session.insert("insertTrailItem", t);
            } else if (t.isDeleted()) {
                rows += session.delete("deleteTrailItem", t.itemId);
            } else {
                rows += session.update("updateTrailItem", t);
            }
        }
        for (TrailAssessmentDto ta : trail.assessments) {
            ta.trailId = trail.trailId;
            if (ta.trailAssessId < 1 && !ta.isDeleted()) {
                session.insert("insertTrailAssessment", ta);
            } else if (ta.isDeleted()) {
                session.delete("deleteTrailAssessment", ta.trailAssessId);
            }
        }
        logActivity(session);
        session.commit();
    } finally {
        session.close();
    }
    return rows;
}

From source file:in.flipbrain.dao.MyBatisDao.java

License:Apache License

public int deleteTrail(Long trailId, Long userId) {
    int rows = 0;
    SqlSession session = sqlSessionFactory.openSession();
    try {//from  w  w w.j  av a2  s. c  o m
        HashMap<String, Object> param = new HashMap<String, Object>();
        param.put("trailId", trailId);
        param.put("createdBy", userId);
        rows = session.delete("deleteTrail", param);
        logActivity(session);
        session.commit();
    } finally {
        session.close();
    }
    return rows;
}

From source file:in.flipbrain.dao.MyBatisDao.java

License:Apache License

public void saveAssessmentSubmission(AssessmentSubmissionDto dto) {
    SqlSession session = sqlSessionFactory.openSession();
    try {/*  www. j ava  2  s.  com*/
        boolean isNew = dto.submissionId < 1 && !dto.isDeleted();
        if (isNew) {
            session.insert("insertAssessmentSubmission", dto);
        } else if (dto.isDeleted()) {
            session.delete("deleteAssessmentSubmission", dto);
        } else {
            session.update("updateAssessmentSubmission", dto);
            session.delete("deleteResponsesForSubmission", dto.submissionId);
        }

        if (!dto.isDeleted()) {
            for (SubmissionResponseDto r : dto.responses) {
                r.submissionId = dto.submissionId;
                if (r.responseId < 1 && !r.isDeleted()) {
                    session.insert("insertSubmissionResponse", r);
                }
            }
        }
        session.commit();
    } finally {
        session.close();
    }
}

From source file:in.flipbrain.dao.MyBatisDao.java

License:Apache License

public void saveQuestion(QuestionDto dto) {
    SqlSession session = sqlSessionFactory.openSession();
    try {/*from w  ww.j a v  a 2  s . c  o  m*/
        if (dto.questionId < 1 && !dto.isDeleted()) {
            session.insert("insertQuestion", dto);
        } else if (dto.isDeleted()) {
            session.delete("deleteQuestion", dto);
        } else {
            session.update("updateQuestion", dto);
        }
        for (AnswerOptionDto opt : dto.answerOptions) {
            opt.questionId = dto.questionId;
            opt.author = dto.author;
            if (opt.answerOptId < 1 && !opt.isDeleted()) {
                session.insert("insertAnswerOption", opt);
            } else if (opt.isDeleted()) {
                session.delete("deleteAnswerOption", opt);
            } else {
                session.update("updateAnswerOption", opt);
            }
        }
        session.commit();
    } finally {
        session.close();
    }
}

From source file:in.flipbrain.dao.MyBatisDao.java

License:Apache License

public void saveAssessment(AssessmentDto dto) {
    SqlSession session = sqlSessionFactory.openSession();
    try {// w  ww  . jav a  2s.  c om
        if (dto.assessId < 1 && !dto.isDeleted()) {
            session.insert("insertAssessment", dto);
        } else if (dto.isDeleted()) {
            session.delete("deleteAssessment", dto);
        } else {
            session.update("updateAssessment", dto);
        }

        for (AssessmentQuestionDto q : dto.questions) {
            q.assessId = dto.assessId;
            if (q.assessQuestId < 1 && !q.isDeleted()) {
                session.insert("insertAssessmentQuestion", q);
            } else if (q.isDeleted()) {
                session.delete("deleteAssessmentQuestion", q);
            } else {
                session.update("updateAssessmentQuestion", q);
            }
        }
        session.commit();
    } finally {
        session.close();
    }
}

From source file:in.flipbrain.dao.MyBatisDao.java

License:Apache License

protected <T> int saveEntity(String stmtId, T dto, Operation op) {
    int rows = 0;
    SqlSession session = sqlSessionFactory.openSession();
    try {//from  w  ww. j  a  va  2  s  . c  o  m
        logActivity(session);
        switch (op) {
        case Del:
            rows = session.delete(stmtId, dto);
            break;
        case Ins:
            rows = session.insert(stmtId, dto);
            break;
        case Upd:
            rows = session.update(stmtId, dto);
            break;
        }
        session.commit();
    } finally {
        session.close();
    }
    return rows;
}

From source file:in.flipbrain.dao.MyBatisDao.java

License:Apache License

protected <P, R> List<R> getEntityList(String stmtId, P dto) {
    SqlSession session = sqlSessionFactory.openSession();
    List<R> result;//from w  w w  .j  ava2  s  .c  o m
    try {
        logActivity(session);
        result = session.selectList(stmtId, dto);
        session.commit();
    } finally {
        session.close();
    }
    return result;
}

From source file:in.flipbrain.dao.MyBatisDao.java

License:Apache License

protected <P, R> R getEntity(String stmtId, P dto) {
    SqlSession session = sqlSessionFactory.openSession();
    R result;/*from w  w  w  . j av a 2 s . co m*/
    try {
        logActivity(session);
        result = session.selectOne(stmtId, dto);
        session.commit();
    } finally {
        session.close();
    }
    return result;
}

From source file:io.starter.datamodel.ContentData.java

License:Open Source License

/**
 * upload a new content item in the system
 * /*  w  w  w .  ja v  a2  s . c  o  m*/
 * @param author
 * @param device
 * @param copyright
 * @param description
 * @param url
 * @param servletRequest
 * @param servletResponse
 * @throws Exception
 */
@PUT
@Path("upload")
@Produces(MediaType.APPLICATION_JSON)
@Consumes({ MediaType.MULTIPART_FORM_DATA, MediaType.APPLICATION_OCTET_STREAM })
public String upload(@FormDataParam("binary_content") FormDataContentDisposition fileDetail,
        @FormDataParam("binary_content") InputStream uploadedInputStream,
        @FormDataParam("userImage") int userImage, @Context HttpServletRequest servletRequest,
        @Context HttpServletResponse servletResponse) throws Exception {

    User u = (User) servletRequest.getSession(true).getAttribute(SESSION_VAR_USER);
    /*
     * TODO: remove mediadir? String mediadir =
     * servletRequest.getSession().getServletContext()
     * .getInitParameter("media.dir");
     */
    String uploadedFileLocation = fileDetail.getFileName();

    // save it
    File[] uploadedFileset = saveToFile(uploadedInputStream, uploadedFileLocation, u.getId());

    Logger.log(
            "UserData.upload() File uploaded via Jersey based RESTFul Webservice to: " + uploadedFileLocation);

    if (userImage > 0) {
        String f = fileDetail.getFileName();
        u.setAvatarImage(f);

        SqlSession session = (SqlSession) servletRequest.getAttribute(SESSION_VAR_SQLSESSION);
        int rowsUpdated = session.update("io.starter.dao.UserMapper.updateByPrimaryKey", u);

        u = UserData.getUserObjectByID(u.getId(), servletRequest, servletResponse);
        u.setAvatarImage(f);

        session.commit();
        if (rowsUpdated < 1)
            throw new ServletException("UserData.upload() could not setAvatarImage()");
    }
    // return Response.status(200).entity(output).build();

    // return the JSON result
    JSONObject j = new JSONObject();

    String fnamey = uploadedFileLocation.substring(0, uploadedFileLocation.lastIndexOf("."));
    String fnamex = uploadedFileLocation.substring(uploadedFileLocation.lastIndexOf("."));

    String fna = "/" + fnamey + "/Standard" + fnamex;

    fna = fnamey + fnamex;
    j.put("filename", fna);

    j.put("mimeType", servletRequest.getSession().getServletContext().getMimeType(fna));

    return j.toString();
}

From source file:io.starter.datamodel.ContentData.java

License:Open Source License

/**
 * //  w  ww.  ja  v  a  2  s. c o m
 * @param servletRequest
 * @param servletResponse
 * @return
 * @throws JSONException
 * @throws IOException
 * @throws ServletException
 */
@DELETE
@Path("delete")
public void delete(@PathParam("contentId") int id, @Context HttpServletRequest servletRequest,
        @Context HttpServletResponse servletResponse) throws IOException, ServletException {

    // TODO: checkaccess
    // Content cxdel = getContentObject(servletRequest, servletResponse);
    // String contentToDelete = cxdel.getUrl();

    // TODO: S3FS is our friend
    // S3FS s3fs = new S3FS();
    // s3fs.deleteFile(S3_STARTER_MEDIA_BUCKET,);

    SqlSession session = (SqlSession) servletRequest.getAttribute(SESSION_VAR_SQLSESSION);

    try {

        // delete content acls
        AclExample a = new AclExample();
        Criteria c = a.createCriteria();
        c.andTargetIdEqualTo(id);
        c.andTargetTypeEqualTo(SystemConstants.SECURITY_TARGET_TYPE_CONTENT);

        session.delete("io.starter.dao.AclMapper.deleteByExample", a);
        session.commit();

        // delete content record
        session.delete("io.starter.dao.ContentMapper.deleteByPrimaryKey", id);
        session.commit();

    } catch (Exception e) {
        // if there are cascading FK constraints then we need to mark this
        // content item deleted (not actually delete it)
        // this way we preserve the integrity of the system
        // TODO: discuss handling deletion of content in re: orphan data

        Content cxdel = getContentObject(servletRequest, id);

        session = (SqlSession) servletRequest.getAttribute(SESSION_VAR_SQLSESSION);

        try {
            cxdel.setFlag(FLAG_DELETED);
            session.update("io.starter.dao.ContentMapper.updateByPrimaryKey", cxdel);
            session.commit();
        } catch (Exception x) {
            Logger.error("Total failure in ContentData.delete trying to mark content item as deleted: "
                    + x.toString());
            // hmmm total failure
        }

    }

    super.deleteds.add(id);
    // update caches
    super.removeContentFromCaches(id, servletRequest);

}