001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     * 
010     *      http://www.apache.org/licenses/LICENSE-2.0
011     * 
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.oozie.workflow.lite;
019    
020    import java.sql.Connection;
021    import java.sql.SQLException;
022    import javax.xml.validation.Schema;
023    
024    import org.apache.oozie.store.OozieSchema.OozieColumn;
025    import org.apache.oozie.store.OozieSchema.OozieTable;
026    import org.apache.oozie.workflow.WorkflowException;
027    import org.apache.oozie.workflow.WorkflowInstance;
028    import org.apache.oozie.util.ParamChecker;
029    import org.apache.oozie.util.WritableUtils;
030    import org.apache.oozie.util.db.SqlStatement.ResultSetReader;
031    import org.apache.oozie.util.db.SqlStatement;
032    import org.apache.oozie.ErrorCode;
033    
034    //TODO javadoc
035    public class DBLiteWorkflowLib extends LiteWorkflowLib {
036        private final Connection connection;
037    
038        public DBLiteWorkflowLib(Schema schema,
039                                 Class<? extends ControlNodeHandler> controlNodeHandler,
040                                 Class<? extends DecisionNodeHandler> decisionHandlerClass,
041                                 Class<? extends ActionNodeHandler> actionHandlerClass, Connection connection) {
042            super(schema, controlNodeHandler, decisionHandlerClass, actionHandlerClass);
043            this.connection = connection;
044        }
045    
046        /**
047         * Save the Workflow Instance for the given Workflow Application.
048         *
049         * @param instance
050         * @return
051         * @throws WorkflowException
052         */
053        @Override
054        public void insert(WorkflowInstance instance) throws WorkflowException {
055            ParamChecker.notNull(instance, "instance");
056            try {
057                SqlStatement.insertInto(OozieTable.WF_PROCESS_INSTANCE).value(OozieColumn.PI_wfId, instance.getId()).value(
058                        OozieColumn.PI_state, WritableUtils.toByteArray((LiteWorkflowInstance) instance))
059                        .prepareAndSetValues(connection).executeUpdate();
060            }
061            catch (SQLException e) {
062                throw new WorkflowException(ErrorCode.E0713, e.getMessage(), e);
063            }
064        }
065    
066        /**
067         * Loads the Workflow instance with the given ID.
068         *
069         * @param id
070         * @return
071         * @throws WorkflowException
072         */
073        @Override
074        public WorkflowInstance get(String id) throws WorkflowException {
075            ParamChecker.notNull(id, "id");
076            try {
077                ResultSetReader rs = SqlStatement.parse(SqlStatement.selectColumns(OozieColumn.PI_state).where(
078                        SqlStatement.isEqual(OozieColumn.PI_wfId, ParamChecker.notNull(id, "id"))).
079                        prepareAndSetValues(connection).executeQuery());
080                rs.next();
081                LiteWorkflowInstance pInstance = WritableUtils.fromByteArray(rs.getByteArray(OozieColumn.PI_state),
082                                                                             LiteWorkflowInstance.class);
083                return pInstance;
084            }
085            catch (SQLException e) {
086                throw new WorkflowException(ErrorCode.E0713, e.getMessage(), e);
087            }
088        }
089    
090        /**
091         * Updates the Workflow Instance to DB.
092         *
093         * @param instance
094         * @throws WorkflowException
095         */
096        @Override
097        public void update(WorkflowInstance instance) throws WorkflowException {
098            ParamChecker.notNull(instance, "instance");
099            try {
100                SqlStatement.update(OozieTable.WF_PROCESS_INSTANCE).set(OozieColumn.PI_state,
101                                                                        WritableUtils.toByteArray((LiteWorkflowInstance) instance)).where(
102                        SqlStatement.isEqual(OozieColumn.PI_wfId, instance.getId())).
103                        prepareAndSetValues(connection).executeUpdate();
104            }
105            catch (SQLException e) {
106                throw new WorkflowException(ErrorCode.E0713, e.getMessage(), e);
107            }
108        }
109    
110        /**
111         * Delets the Workflow Instance with the given id.
112         *
113         * @param id
114         * @throws WorkflowException
115         */
116        @Override
117        public void delete(String id) throws WorkflowException {
118            ParamChecker.notNull(id, "id");
119            try {
120                SqlStatement.deleteFrom(OozieTable.WF_PROCESS_INSTANCE).where(
121                        SqlStatement.isEqual(OozieColumn.PI_wfId, id)).prepareAndSetValues(connection).executeUpdate();
122            }
123            catch (SQLException e) {
124                throw new WorkflowException(ErrorCode.E0713, e.getMessage(), e);
125            }
126        }
127    
128        @Override
129        public void commit() throws WorkflowException {
130            // NOP
131        }
132    
133        @Override
134        public void close() throws WorkflowException {
135            // NOP
136        }
137    }