001/**
002 * Copyright 2005-2017 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kew.rule;
017
018import org.apache.commons.lang.ObjectUtils;
019import org.kuali.rice.kew.api.rule.RuleExpression;
020import org.kuali.rice.kew.api.rule.RuleExpressionContract;
021import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
022import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
023
024import javax.persistence.Column;
025import javax.persistence.Entity;
026import javax.persistence.GeneratedValue;
027import javax.persistence.Id;
028import javax.persistence.Table;
029
030/**
031 * BO for rule expressions 
032 * @author Kuali Rice Team (rice.collab@kuali.org)
033 */
034@Entity
035@Table(name="KREW_RULE_EXPR_T")
036//@Sequence(name="KREW_RULE_EXPR_S", property="id")
037public class RuleExpressionDef extends PersistableBusinessObjectBase implements RuleExpressionContract {
038    
039    /**
040     * Primary key
041     */
042    @Id
043    @PortableSequenceGenerator(name="KREW_RULE_EXPR_S")
044    @GeneratedValue(generator="KREW_RULE_EXPR_S")
045        @Column(name="RULE_EXPR_ID")
046        private String id;
047    /**
048     * The type of the expression
049     */
050    @Column(name="TYP")
051        private String type;
052    /**
053     * The content of the expression
054     */
055    @Column(name="RULE_EXPR", nullable=true)
056        private String expression;
057    /**
058     * @return the id
059     */
060    public String getId() {
061        return this.id;
062    }
063    /**
064     * @param id the id to set
065     */
066    public void setId(String id) {
067        this.id = id;
068    }
069    /**
070     * @return the type
071     */
072    public String getType() {
073        return this.type;
074    }
075    /**
076     * @param type the type to set
077     */
078    public void setType(String type) {
079        this.type = type;
080    }
081    /**
082     * @return the expression
083     */
084    public String getExpression() {
085        return this.expression;
086    }
087    /**
088     * @param expression the expression to set
089     */
090    public void setExpression(String expression) {
091        this.expression = expression;
092    }
093
094    /**
095     * Returns whether the object is an <i>equivalent</i> rule expression, i.e.
096     * the type and expression are the same.  This is necessary for rule duplicate
097     * detection.
098     * @see java.lang.Object#equals(java.lang.Object)
099     */
100    @Override
101    public boolean equals(Object obj) {
102        if (obj == null) return false;
103        if (!(obj instanceof RuleExpressionDef)) return false;
104        RuleExpressionDef arg = (RuleExpressionDef) obj;
105        return ObjectUtils.equals(type, arg.getType()) && ObjectUtils.equals(expression, arg.getExpression());
106    }
107
108    /**
109     * Converts a mutable bo to its immutable counterpart
110     * @param bo the mutable business object
111     * @return the immutable object
112     */
113    public static org.kuali.rice.kew.api.rule.RuleExpression to(RuleExpressionDef bo) {
114        if (bo == null) {
115            return null;
116        }
117
118        return RuleExpression.Builder.create(bo).build();
119    }
120
121    /**
122     * Converts a immutable object to its mutable counterpart
123     * @param im immutable object
124     * @return the mutable bo
125     */
126    public static RuleExpressionDef from(RuleExpression im) {
127        if (im == null) {
128            return null;
129        }
130
131        RuleExpressionDef bo = new RuleExpressionDef();
132        bo.setId(im.getId());
133        bo.setType(im.getType());
134        bo.setExpression(im.getExpression());
135        bo.setVersionNumber(im.getVersionNumber());
136        bo.setObjectId(im.getObjectId());
137
138        return bo;
139    }
140}