Coverage Report - org.apache.camel.bam.model.ProcessDefinition
 
Classes in this File Line Coverage Branch Coverage Complexity
ProcessDefinition
55% 
50% 
0
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.bam.model;
 18  
 
 19  
 import java.util.List;
 20  
 
 21  
 import javax.persistence.Entity;
 22  
 import javax.persistence.GeneratedValue;
 23  
 import javax.persistence.Id;
 24  
 import javax.persistence.UniqueConstraint;
 25  
 
 26  
 import org.apache.camel.util.ObjectHelper;
 27  
 import org.apache.commons.logging.Log;
 28  
 import org.apache.commons.logging.LogFactory;
 29  
 
 30  
 import org.springframework.orm.jpa.JpaTemplate;
 31  
 
 32  
 /**
 33  
  * @version $Revision: 1.1 $
 34  
  */
 35  
 @Entity
 36  
 @UniqueConstraint(columnNames = {"name" })
 37  12
 public class ProcessDefinition extends EntitySupport {
 38  2
     private static final transient Log LOG = LogFactory.getLog(ProcessDefinition.class);
 39  
     private String name;
 40  
 
 41  
     // This crap is required to work around a bug in hibernate
 42  
     @Override
 43  
     @Id
 44  
     @GeneratedValue
 45  
     public Long getId() {
 46  82
         return super.getId();
 47  
     }
 48  
 
 49  
     public String getName() {
 50  86
         return name;
 51  
     }
 52  
 
 53  
     public void setName(String name) {
 54  6
         this.name = name;
 55  6
     }
 56  
 
 57  
     public static ProcessDefinition getRefreshedProcessDefinition(JpaTemplate template, ProcessDefinition definition) {
 58  
         // TODO refresh doesn't tend to work - maybe its a spring thing?
 59  
         // template.refresh(definition);
 60  
 
 61  8
         ObjectHelper.notNull(definition, "definition");
 62  8
         Long id = definition.getId();
 63  8
         if (id == null) {
 64  0
             LOG.warn("No primary key is available!");
 65  0
             return findOrCreateProcessDefinition(template, definition.getName());
 66  
         }
 67  8
         definition = template.find(ProcessDefinition.class, id);
 68  8
         return definition;
 69  
     }
 70  
 
 71  
     public static ProcessDefinition findOrCreateProcessDefinition(JpaTemplate template, String processName) {
 72  0
         List<ProcessDefinition> list = template.find("select x from " + ProcessDefinition.class.getName() + " x where x.name = ?1", processName);
 73  0
         if (!list.isEmpty()) {
 74  0
             return list.get(0);
 75  
         } else {
 76  0
             ProcessDefinition answer = new ProcessDefinition();
 77  0
             answer.setName(processName);
 78  0
             template.persist(answer);
 79  0
             return answer;
 80  
         }
 81  
     }
 82  
 }