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 * @return 052 * @throws WorkflowException 053 */ 054 @Override 055 public void insert(WorkflowInstance instance) throws WorkflowException { 056 ParamChecker.notNull(instance, "instance"); 057 try { 058 SqlStatement.insertInto(OozieTable.WF_PROCESS_INSTANCE).value(OozieColumn.PI_wfId, instance.getId()).value( 059 OozieColumn.PI_state, WritableUtils.toByteArray((LiteWorkflowInstance) instance)) 060 .prepareAndSetValues(connection).executeUpdate(); 061 } 062 catch (SQLException e) { 063 throw new WorkflowException(ErrorCode.E0713, e.getMessage(), e); 064 } 065 } 066 067 /** 068 * Loads the Workflow instance with the given ID. 069 * 070 * @param id 071 * @return 072 * @throws WorkflowException 073 */ 074 @Override 075 public WorkflowInstance get(String id) throws WorkflowException { 076 ParamChecker.notNull(id, "id"); 077 try { 078 ResultSetReader rs = SqlStatement.parse(SqlStatement.selectColumns(OozieColumn.PI_state).where( 079 SqlStatement.isEqual(OozieColumn.PI_wfId, ParamChecker.notNull(id, "id"))). 080 prepareAndSetValues(connection).executeQuery()); 081 rs.next(); 082 LiteWorkflowInstance pInstance = WritableUtils.fromByteArray(rs.getByteArray(OozieColumn.PI_state), 083 LiteWorkflowInstance.class); 084 return pInstance; 085 } 086 catch (SQLException e) { 087 throw new WorkflowException(ErrorCode.E0713, e.getMessage(), e); 088 } 089 } 090 091 /** 092 * Updates the Workflow Instance to DB. 093 * 094 * @param instance 095 * @throws WorkflowException 096 */ 097 @Override 098 public void update(WorkflowInstance instance) throws WorkflowException { 099 ParamChecker.notNull(instance, "instance"); 100 try { 101 SqlStatement.update(OozieTable.WF_PROCESS_INSTANCE).set(OozieColumn.PI_state, 102 WritableUtils.toByteArray((LiteWorkflowInstance) instance)).where( 103 SqlStatement.isEqual(OozieColumn.PI_wfId, instance.getId())). 104 prepareAndSetValues(connection).executeUpdate(); 105 } 106 catch (SQLException e) { 107 throw new WorkflowException(ErrorCode.E0713, e.getMessage(), e); 108 } 109 } 110 111 /** 112 * Delets the Workflow Instance with the given id. 113 * 114 * @param id 115 * @throws WorkflowException 116 */ 117 @Override 118 public void delete(String id) throws WorkflowException { 119 ParamChecker.notNull(id, "id"); 120 try { 121 SqlStatement.deleteFrom(OozieTable.WF_PROCESS_INSTANCE).where( 122 SqlStatement.isEqual(OozieColumn.PI_wfId, id)).prepareAndSetValues(connection).executeUpdate(); 123 } 124 catch (SQLException e) { 125 throw new WorkflowException(ErrorCode.E0713, e.getMessage(), e); 126 } 127 } 128 129 @Override 130 public void commit() throws WorkflowException { 131 // NOP 132 } 133 134 @Override 135 public void close() throws WorkflowException { 136 // NOP 137 } 138}