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