001: package com.teamkonzept.field.db.queries;
002:
003: import com.teamkonzept.db.*;
004: import com.teamkonzept.webman.mainint.db.queries.field.SelectMaxVersionIdFromContentVersion;
005: import com.teamkonzept.webman.mainint.db.queries.field.SelectMaxContentIdFromContent;
006: import com.teamkonzept.webman.mainint.db.queries.field.InsertContentIdContent;
007: import com.teamkonzept.webman.mainint.db.queries.content.InsertContentVersion;
008: import com.teamkonzept.webman.mainint.db.queries.field.SelectFromContentVersion;
009:
010: import java.sql.*;
011:
012: import org.apache.log4j.Category;
013:
014: /**
015: * @author Markus Freitag
016: * @version 1.0
017: */
018: public class TKDBContentNew extends TKExtendedPrepQuery {
019:
020: /** Logging Category */
021: private static Category cat = Category
022: .getInstance(TKDBContentNew.class.getName());
023: public final static boolean IS_PREPARED = true;
024: public final static String[] PARAM_ORDER = { "INSTANCE_ID",
025: "STATUS_ID", "VERS_INFO", "VERS_AUTHOR" };
026:
027: public final static Object[][] PARAM_TYPES = {
028: { "VERS_INFO", new Integer(Types.VARCHAR) },
029: { "VERS_AUTHOR", new Integer(Types.VARCHAR) } };
030:
031: public final static boolean[] SET_RELEVANTS = { true };
032:
033: protected static Class[] queryClasses = {
034: SelectMaxVersionIdFromContentVersion.class, //[0]
035: SelectMaxContentIdFromContent.class, //[1]
036: InsertContentIdContent.class, //[2]
037: InsertContentVersion.class, //[3]
038: SelectFromContentVersion.class //[4]
039: };
040:
041: public boolean execute() {
042:
043: try {
044: init(queryClasses); // init query objects
045:
046: boolean isNotOpen = aTKDBConnection.isAutoCommit();
047: if (isNotOpen) {
048: TKDBManager.beginTransaction(); // begin transaction
049: }
050:
051: ResultSet rs = null;
052:
053: // get max(version_id) from content_version
054: queries[0].execute();
055: rs = queries[0].fetchResultSet();
056: int next_version_id = 1;
057: if (rs.next()) {
058: next_version_id = rs.getInt(1) + 1;
059: }
060: Integer versionId = new Integer(next_version_id);
061:
062: // get max(content_id) from content
063: queries[1].execute();
064: rs = queries[1].fetchResultSet();
065: int next_content_id = 1;
066: if (rs.next()) {
067: next_content_id = rs.getInt(1) + 1;
068: }
069: Integer contentId = new Integer(next_content_id);
070:
071: // insert into content
072: queries[2].setQueryParams("CONT_ID", contentId);
073: queries[2].execute();
074:
075: // insert into content_version
076: //"VERSION_ID", "INSTANCE_ID", "CONTENT_ID", "STATUS_ID", "DATE", "VERS_INFO", "VERS_AUTHOR"
077: queries[3].setQueryParams("VERSION_ID", versionId);
078: queries[3].setQueryParams("INSTANCE_ID", queryParams
079: .get("INSTANCE_ID"));
080: queries[3].setQueryParams("CONTENT_ID", contentId);
081: queries[3].setQueryParams("STATUS_ID", queryParams
082: .get("STATUS_ID"));
083: queries[3].setQueryParams("DATE", new Timestamp(System
084: .currentTimeMillis()));
085: queries[3].setQueryParams("VERS_INFO", queryParams
086: .get("VERS_INFO"));
087: queries[3].setQueryParams("VERS_AUTHOR", queryParams
088: .get("VERS_AUTHOR"));
089: queries[3].execute();
090:
091: // SELECT CV.*, CI.NAME FROM CONTENT_VERSION CV, CONTENT_INSTANCE CI WHERE CV.VERSION_ID = ? AND CI.INSTANCE_ID = ?
092: queries[4].setQueryParams("VERS_ID", versionId);
093: queries[4].setQueryParams("INST_ID", queryParams
094: .get("INSTANCE_ID"));
095: queries[4].execute();
096: rs = queries[4].fetchResultSet();
097: addResult(rs);
098:
099: // COMMIT TRANSACTION
100: if (isNotOpen) {
101: aTKDBConnection.commitTransaction(); // commit all changes
102: }
103:
104: } catch (Throwable t) {
105: cat.error("Error while inserting", t);
106: TKDBManager.safeRollbackTransaction(t);
107: }
108: return hasResults();
109: }
110:
111: public void initQuery(Connection con) {
112: super.initQuery(con, IS_PREPARED, PARAM_ORDER, PARAM_TYPES,
113: SET_RELEVANTS, null);
114: }
115: }
|