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