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
019package org.apache.oozie.workflow.lite;
020
021import java.sql.Connection;
022import java.sql.SQLException;
023import javax.xml.validation.Schema;
024
025import org.apache.oozie.store.OozieSchema.OozieColumn;
026import org.apache.oozie.store.OozieSchema.OozieTable;
027import org.apache.oozie.workflow.WorkflowException;
028import org.apache.oozie.workflow.WorkflowInstance;
029import org.apache.oozie.util.ParamChecker;
030import org.apache.oozie.util.WritableUtils;
031import org.apache.oozie.util.db.SqlStatement.ResultSetReader;
032import org.apache.oozie.util.db.SqlStatement;
033import org.apache.oozie.ErrorCode;
034
035//TODO javadoc
036public class DBLiteWorkflowLib extends LiteWorkflowLib {
037    private final Connection connection;
038
039    public DBLiteWorkflowLib(Schema schema,
040                             Class<? extends ControlNodeHandler> controlNodeHandler,
041                             Class<? extends DecisionNodeHandler> decisionHandlerClass,
042                             Class<? extends ActionNodeHandler> actionHandlerClass, Connection connection) {
043        super(schema, controlNodeHandler, decisionHandlerClass, actionHandlerClass);
044        this.connection = connection;
045    }
046
047    /**
048     * Save the Workflow Instance for the given Workflow Application.
049     *
050     * @param instance
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}