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; 016 017 import org.apache.oozie.workflow.WorkflowInstance; 018 import org.apache.oozie.workflow.lite.LiteWorkflowInstance; 019 import org.apache.oozie.client.rest.JsonWorkflowJob; 020 import org.apache.oozie.client.WorkflowJob; 021 import org.apache.oozie.util.DateUtils; 022 import org.apache.oozie.util.WritableUtils; 023 import org.apache.hadoop.io.Writable; 024 025 import java.io.DataInput; 026 import java.io.IOException; 027 import java.io.DataOutput; 028 import java.util.Date; 029 030 import javax.persistence.Entity; 031 import javax.persistence.Column; 032 import javax.persistence.NamedQueries; 033 import javax.persistence.NamedQuery; 034 import javax.persistence.Basic; 035 import javax.persistence.Lob; 036 037 import java.sql.Timestamp; 038 039 import org.apache.openjpa.persistence.jdbc.Index; 040 041 @Entity 042 @NamedQueries({ 043 044 @NamedQuery(name = "UPDATE_WORKFLOW", query = "update WorkflowJobBean w set w.appName = :appName, w.appPath = :appPath, w.conf = :conf, w.group = :groupName, w.run = :run, w.user = :user, w.authToken = :authToken, w.createdTimestamp = :createdTime, w.endTimestamp = :endTime, w.externalId = :externalId, w.lastModifiedTimestamp = :lastModTime, w.logToken = :logToken, w.protoActionConf = :protoActionConf, w.slaXml =:slaXml, w.startTimestamp = :startTime, w.status = :status, w.wfInstance = :wfInstance where w.id = :id"), 045 046 @NamedQuery(name = "DELETE_WORKFLOW", query = "delete from WorkflowJobBean w where w.id = :id"), 047 048 @NamedQuery(name = "GET_WORKFLOWS", query = "select OBJECT(w) from WorkflowJobBean w order by w.startTimestamp desc"), 049 050 @NamedQuery(name = "GET_WORKFLOWS_COLUMNS", query = "select w.id, w.appName, w.status, w.run, w.user, w.group, w.createdTimestamp, " 051 + "w.startTimestamp, w.lastModifiedTimestamp, w.endTimestamp from WorkflowJobBean w order by w.startTimestamp desc"), 052 053 @NamedQuery(name = "GET_WORKFLOWS_COUNT", query = "select count(w) from WorkflowJobBean w"), 054 055 @NamedQuery(name = "GET_COMPLETED_WORKFLOWS_OLDER_THAN", query = "select w from WorkflowJobBean w where w.endTimestamp < :endTime"), 056 057 @NamedQuery(name = "GET_WORKFLOW", query = "select OBJECT(w) from WorkflowJobBean w where w.id = :id"), 058 059 @NamedQuery(name = "GET_WORKFLOW_FOR_UPDATE", query = "select OBJECT(w) from WorkflowJobBean w where w.id = :id"), 060 061 @NamedQuery(name = "GET_WORKFLOW_ID_FOR_EXTERNAL_ID", query = "select w.id from WorkflowJobBean w where w.externalId = :externalId"), 062 063 @NamedQuery(name = "GET_WORKFLOWS_COUNT_WITH_STATUS", query = "select count(w) from WorkflowJobBean w where w.status = :status"), 064 065 @NamedQuery(name = "GET_WORKFLOWS_COUNT_WITH_STATUS_IN_LAST_N_SECS", query = "select count(w) from WorkflowJobBean w where w.status = :status and w.lastModifiedTimestamp > :lastModTime") 066 067 }) 068 public class WorkflowJobBean extends JsonWorkflowJob implements Writable { 069 070 @Column(name = "proto_action_conf") 071 @Lob 072 private String protoActionConf = null; 073 074 @Basic 075 @Column(name = "log_token") 076 private String logToken = null; 077 078 @Basic 079 @Index 080 @Column(name = "external_id") 081 private String externalId = null; 082 083 @Basic 084 @Index 085 @Column(name = "status") 086 private String status = WorkflowJob.Status.PREP.toString(); 087 088 @Basic 089 @Column(name = "created_time") 090 private java.sql.Timestamp createdTimestamp = null; 091 092 @Basic 093 @Column(name = "start_time") 094 private java.sql.Timestamp startTimestamp = null; 095 096 @Basic 097 @Index 098 @Column(name = "end_time") 099 private java.sql.Timestamp endTimestamp = null; 100 101 @Column(name = "auth_token") 102 @Lob 103 private String authToken = null; 104 105 @Basic 106 @Index 107 @Column(name = "last_modified_time") 108 private java.sql.Timestamp lastModifiedTimestamp = null; 109 110 // @Basic(fetch = FetchType.LAZY) 111 // @Column(name="wfinstance",columnDefinition="blob") 112 @Column(name = "wf_instance") 113 @Lob 114 private byte[] wfInstance = null; 115 116 @Column(name = "sla_xml") 117 @Lob 118 private String slaXml = null; 119 120 /** 121 * Default constructor. 122 */ 123 public WorkflowJobBean() { 124 } 125 126 /** 127 * Serialize the workflow bean to a data output. 128 * 129 * @param dataOutput data output. 130 * @throws IOException thrown if the workflow bean could not be serialized. 131 */ 132 public void write(DataOutput dataOutput) throws IOException { 133 WritableUtils.writeStr(dataOutput, getAppPath()); 134 WritableUtils.writeStr(dataOutput, getAppName()); 135 WritableUtils.writeStr(dataOutput, getId()); 136 WritableUtils.writeStr(dataOutput, getConf()); 137 WritableUtils.writeStr(dataOutput, getStatusStr()); 138 dataOutput.writeLong((getCreatedTime() != null) ? getCreatedTime().getTime() : -1); 139 dataOutput.writeLong((getStartTime() != null) ? getStartTime().getTime() : -1); 140 dataOutput.writeLong((getLastModifiedTime() != null) ? getLastModifiedTime().getTime() : -1); 141 dataOutput.writeLong((getEndTime() != null) ? getEndTime().getTime() : -1); 142 WritableUtils.writeStr(dataOutput, getUser()); 143 WritableUtils.writeStr(dataOutput, getGroup()); 144 dataOutput.writeInt(getRun()); 145 WritableUtils.writeStr(dataOutput, authToken); 146 WritableUtils.writeStr(dataOutput, logToken); 147 WritableUtils.writeStr(dataOutput, protoActionConf); 148 } 149 150 /** 151 * Deserialize a workflow bean from a data input. 152 * 153 * @param dataInput data input. 154 * @throws IOException thrown if the workflow bean could not be deserialized. 155 */ 156 public void readFields(DataInput dataInput) throws IOException { 157 setAppPath(WritableUtils.readStr(dataInput)); 158 setAppName(WritableUtils.readStr(dataInput)); 159 setId(WritableUtils.readStr(dataInput)); 160 setConf(WritableUtils.readStr(dataInput)); 161 setStatus(WorkflowJob.Status.valueOf(WritableUtils.readStr(dataInput))); 162 // setStatus(WritableUtils.readStr(dataInput)); 163 long d = dataInput.readLong(); 164 if (d != -1) { 165 setCreatedTime(new Date(d)); 166 } 167 d = dataInput.readLong(); 168 if (d != -1) { 169 } 170 setStartTime(new Date(d)); 171 d = dataInput.readLong(); 172 if (d != -1) { 173 setLastModifiedTime(new Date(d)); 174 } 175 d = dataInput.readLong(); 176 if (d != -1) { 177 setEndTime(new Date(d)); 178 } 179 setUser(WritableUtils.readStr(dataInput)); 180 setGroup(WritableUtils.readStr(dataInput)); 181 setRun(dataInput.readInt()); 182 authToken = WritableUtils.readStr(dataInput); 183 logToken = WritableUtils.readStr(dataInput); 184 protoActionConf = WritableUtils.readStr(dataInput); 185 setExternalId(getExternalId()); 186 setProtoActionConf(protoActionConf); 187 } 188 189 public String getAuthToken() { 190 return authToken; 191 } 192 193 public void setAuthToken(String authToken) { 194 this.authToken = authToken; 195 } 196 197 public String getLogToken() { 198 return logToken; 199 } 200 201 public void setLogToken(String logToken) { 202 this.logToken = logToken; 203 } 204 205 public String getSlaXml() { 206 return slaXml; 207 } 208 209 public void setSlaXml(String slaXml) { 210 this.slaXml = slaXml; 211 } 212 213 public WorkflowInstance getWorkflowInstance() { 214 return get(this.wfInstance); 215 } 216 217 public byte[] getWfInstance() { 218 return wfInstance; 219 } 220 221 public void setWorkflowInstance(WorkflowInstance workflowInstance) { 222 setWfInstance(workflowInstance); 223 } 224 225 public void setWfInstance(byte[] wfInstance) { 226 this.wfInstance = wfInstance; 227 } 228 229 public void setWfInstance(WorkflowInstance wfInstance) { 230 this.wfInstance = WritableUtils.toByteArray((LiteWorkflowInstance) wfInstance); 231 } 232 233 public String getProtoActionConf() { 234 return protoActionConf; 235 } 236 237 public void setProtoActionConf(String protoActionConf) { 238 this.protoActionConf = protoActionConf; 239 } 240 241 public String getprotoActionConf() { 242 return protoActionConf; 243 } 244 245 public String getlogToken() { 246 return logToken; 247 } 248 249 public String getStatusStr() { 250 return status; 251 } 252 253 public Timestamp getLastModifiedTimestamp() { 254 return lastModifiedTimestamp; 255 } 256 257 public Timestamp getStartTimestamp() { 258 return startTimestamp; 259 } 260 261 public Timestamp getCreatedTimestamp() { 262 return createdTimestamp; 263 } 264 265 public Timestamp getEndTimestamp() { 266 return endTimestamp; 267 } 268 269 @Override 270 public void setAppName(String val) { 271 super.setAppName(val); 272 } 273 274 @Override 275 public void setAppPath(String val) { 276 super.setAppPath(val); 277 } 278 279 @Override 280 public void setConf(String val) { 281 super.setConf(val); 282 } 283 284 @Override 285 public void setStatus(Status val) { 286 super.setStatus(val); 287 this.status = val.toString(); 288 } 289 290 @Override 291 public Status getStatus() { 292 return Status.valueOf(this.status); 293 } 294 295 @Override 296 public void setExternalId(String externalId) { 297 super.setExternalId(externalId); 298 this.externalId = externalId; 299 } 300 301 @Override 302 public String getExternalId() { 303 return externalId; 304 } 305 306 @Override 307 public void setLastModifiedTime(Date lastModifiedTime) { 308 super.setLastModifiedTime(lastModifiedTime); 309 this.lastModifiedTimestamp = DateUtils.convertDateToTimestamp(lastModifiedTime); 310 } 311 312 @Override 313 public Date getLastModifiedTime() { 314 return DateUtils.toDate(lastModifiedTimestamp); 315 } 316 317 @Override 318 public Date getCreatedTime() { 319 return DateUtils.toDate(createdTimestamp); 320 } 321 322 @Override 323 public void setCreatedTime(Date createdTime) { 324 super.setCreatedTime(createdTime); 325 this.createdTimestamp = DateUtils.convertDateToTimestamp(createdTime); 326 } 327 328 @Override 329 public Date getStartTime() { 330 return DateUtils.toDate(startTimestamp); 331 } 332 333 @Override 334 public void setStartTime(Date startTime) { 335 super.setStartTime(startTime); 336 this.startTimestamp = DateUtils.convertDateToTimestamp(startTime); 337 } 338 339 @Override 340 public Date getEndTime() { 341 return DateUtils.toDate(endTimestamp); 342 } 343 344 @Override 345 public void setEndTime(Date endTime) { 346 super.setEndTime(endTime); 347 this.endTimestamp = DateUtils.convertDateToTimestamp(endTime); 348 } 349 350 private WorkflowInstance get(byte[] array) { 351 LiteWorkflowInstance pInstance = WritableUtils.fromByteArray(array, LiteWorkflowInstance.class); 352 return pInstance; 353 } 354 355 }