001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.camel.bam.model; 018 019 import java.util.List; 020 021 import javax.persistence.Entity; 022 import javax.persistence.GeneratedValue; 023 import javax.persistence.Id; 024 import javax.persistence.UniqueConstraint; 025 026 import org.apache.camel.util.ObjectHelper; 027 import org.apache.commons.logging.Log; 028 import org.apache.commons.logging.LogFactory; 029 030 import org.springframework.orm.jpa.JpaTemplate; 031 032 /** 033 * @version $Revision: 1.1 $ 034 */ 035 @Entity 036 @UniqueConstraint(columnNames = {"name" }) 037 public class ProcessDefinition extends EntitySupport { 038 private static final transient Log LOG = LogFactory.getLog(ProcessDefinition.class); 039 private String name; 040 041 // This crap is required to work around a bug in hibernate 042 @Override 043 @Id 044 @GeneratedValue 045 public Long getId() { 046 return super.getId(); 047 } 048 049 public String getName() { 050 return name; 051 } 052 053 public void setName(String name) { 054 this.name = name; 055 } 056 057 public static ProcessDefinition getRefreshedProcessDefinition(JpaTemplate template, ProcessDefinition definition) { 058 // TODO refresh doesn't tend to work - maybe its a spring thing? 059 // template.refresh(definition); 060 061 ObjectHelper.notNull(definition, "definition"); 062 Long id = definition.getId(); 063 if (id == null) { 064 LOG.warn("No primary key is available!"); 065 return findOrCreateProcessDefinition(template, definition.getName()); 066 } 067 definition = template.find(ProcessDefinition.class, id); 068 return definition; 069 } 070 071 public static ProcessDefinition findOrCreateProcessDefinition(JpaTemplate template, String processName) { 072 List<ProcessDefinition> list = template.find("select x from " + ProcessDefinition.class.getName() + " x where x.name = ?1", processName); 073 if (!list.isEmpty()) { 074 return list.get(0); 075 } else { 076 ProcessDefinition answer = new ProcessDefinition(); 077 answer.setName(processName); 078 template.persist(answer); 079 return answer; 080 } 081 } 082 }