001    /**
002     * Copyright 2005-2013 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     */
016    package org.kuali.rice.kew.rule;
017    
018    import org.apache.commons.lang.ObjectUtils;
019    import org.hibernate.annotations.GenericGenerator;
020    import org.hibernate.annotations.Parameter;
021    import org.kuali.rice.kew.api.rule.*;
022    import org.kuali.rice.kew.api.rule.RuleExpression;
023    import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
024    
025    import javax.persistence.*;
026    
027    /**
028     * BO for rule expressions 
029     * @author Kuali Rice Team (rice.collab@kuali.org)
030     */
031    @Entity
032    @Table(name="KREW_RULE_EXPR_T")
033    //@Sequence(name="KREW_RULE_EXPR_S", property="id")
034    public class RuleExpressionDef extends PersistableBusinessObjectBase implements RuleExpressionContract {
035        
036        /**
037         * Primary key
038         */
039        @Id
040        @GeneratedValue(generator="KREW_RULE_EXPR_S")
041            @GenericGenerator(name="KREW_RULE_EXPR_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
042                            @Parameter(name="sequence_name",value="KREW_RULE_EXPR_S"),
043                            @Parameter(name="value_column",value="id")
044            })
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    }