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