001package org.andromda.cartridges.spring.metafacades;
002
003import org.andromda.cartridges.spring.SpringProfile;
004import org.apache.commons.lang.BooleanUtils;
005import org.apache.commons.lang.ObjectUtils;
006import org.apache.commons.lang.StringUtils;
007
008/**
009 * MetafacadeLogic implementation for org.andromda.cartridges.spring.metafacades.SpringCriteriaAttribute.
010 *
011 * @see org.andromda.cartridges.spring.metafacades.SpringCriteriaAttribute
012 */
013public class SpringCriteriaAttributeLogicImpl
014    extends SpringCriteriaAttributeLogic
015{
016    private static final long serialVersionUID = 34L;
017    /**
018     * Public constructor for SpringCriteriaAttributeLogicImpl
019     * @param metaObject
020     * @param context
021     * @see org.andromda.cartridges.spring.metafacades.SpringCriteriaAttribute
022     */
023    public SpringCriteriaAttributeLogicImpl(
024        Object metaObject,
025        String context)
026    {
027        super(metaObject, context);
028    }
029
030    /**
031     * @return attributeName
032     * @see org.andromda.cartridges.spring.metafacades.SpringCriteriaAttribute#getAttributeName()
033     */
034    protected String handleGetAttributeName()
035    {
036        // use the attribute name by default
037        String attributeName = getName();
038
039        // if there is a tagged value, use it instead
040        Object value = findTaggedValue(SpringProfile.TAGGEDVALUE_HIBERNATE_CRITERIA_ATTRIBUTE);
041        if (value != null)
042        {
043            attributeName = String.valueOf(value);
044        }
045
046        return StringUtils.trimToEmpty(attributeName);
047    }
048
049    /**
050     * @return comparator
051     * @see org.andromda.cartridges.spring.metafacades.SpringCriteriaAttribute#getComparator()
052     */
053    protected String handleGetComparator()
054    {
055        String comparator = null;
056        Object value = findTaggedValue(SpringProfile.TAGGEDVALUE_HIBERNATE_CRITERIA_COMPARATOR);
057        if (value != null)
058        {
059            comparator = String.valueOf(value);
060        }
061        return StringUtils.trimToEmpty(comparator);
062    }
063
064    /**
065     * @return !StringUtils.isBlank(getComparator())
066     * @see org.andromda.cartridges.spring.metafacades.SpringCriteriaAttribute#isComparatorPresent()
067     */
068    protected boolean handleIsComparatorPresent()
069    {
070        return !StringUtils.isBlank(getComparator());
071    }
072
073    /**
074     * @return comparatorConstant
075     * @see org.andromda.cartridges.spring.metafacades.SpringCriteriaAttribute#getComparatorConstant()
076     */
077    protected String handleGetComparatorConstant()
078    {
079        String comparator = getComparator();
080        String comparatorConstant = null;
081
082        if (comparator != null)
083        {
084            if (SpringProfile.TAGGEDVALUEVALUE_COMPARATOR_LIKE.equalsIgnoreCase(comparator))
085            {
086                comparatorConstant = "LIKE_COMPARATOR";
087            }
088            else if (SpringProfile.TAGGEDVALUEVALUE_INSENSITIVE_LIKE_COMPARATOR.equalsIgnoreCase(comparator))
089            {
090                comparatorConstant = "INSENSITIVE_LIKE_COMPARATOR";
091            }
092            else if (SpringProfile.TAGGEDVALUEVALUE_COMPARATOR_EQUAL.equalsIgnoreCase(comparator))
093            {
094                comparatorConstant = "EQUAL_COMPARATOR";
095            }
096            else if (SpringProfile.TAGGEDVALUEVALUE_COMPARATOR_GREATER_OR_EQUAL.equalsIgnoreCase(comparator))
097            {
098                comparatorConstant = "GREATER_THAN_OR_EQUAL_COMPARATOR";
099            }
100            else if (SpringProfile.TAGGEDVALUEVALUE_COMPARATOR_GREATER.equalsIgnoreCase(comparator))
101            {
102                comparatorConstant = "GREATER_THAN_COMPARATOR";
103            }
104            else if (SpringProfile.TAGGEDVALUEVALUE_COMPARATOR_LESS_OR_EQUAL.equalsIgnoreCase(comparator))
105            {
106                comparatorConstant = "LESS_THAN_OR_EQUAL_COMPARATOR";
107            }
108            else if (SpringProfile.TAGGEDVALUEVALUE_COMPARATOR_LESS.equalsIgnoreCase(comparator))
109            {
110                comparatorConstant = "LESS_THAN_COMPARATOR";
111            }
112            else if (SpringProfile.TAGGEDVALUEVALUE_COMPARATOR_IN.equalsIgnoreCase(comparator))
113            {
114                comparatorConstant = "IN_COMPARATOR";
115            }
116            else if (SpringProfile.TAGGEDVALUEVALUE_COMPARATOR_NOT_EQUAL.equalsIgnoreCase(comparator))
117            {
118                comparatorConstant = "NOT_EQUAL_COMPARATOR";
119            }
120        }
121        return comparatorConstant;
122    }
123
124    /**
125     * @see org.andromda.cartridges.spring.metafacades.SpringCriteriaAttributeLogic#handleIsNullable()
126     */
127    protected boolean handleIsNullable()
128    {
129        boolean result = false;
130        String value =
131            StringUtils.trimToEmpty((String)findTaggedValue(SpringProfile.TAGGEDVALUE_HIBERNATE_CRITERIA_NULLABLE));
132        if (!StringUtils.isBlank(value))
133        {
134            result = BooleanUtils.toBoolean(value);
135        }
136        return result;
137    }
138
139    /**
140     * @see org.andromda.cartridges.spring.metafacades.SpringCriteriaAttributeLogic#handleGetMatchMode()
141     */
142    protected String handleGetMatchMode()
143    {
144        String matchMode = null;
145        Object value = findTaggedValue(SpringProfile.TAGGEDVALUE_HIBERNATE_CRITERIA_MATCHMODE);
146        if (value != null)
147        {
148            matchMode = String.valueOf(value);
149        }
150        return StringUtils.trimToEmpty(matchMode);
151    }
152
153    /**
154     * @see org.andromda.cartridges.spring.metafacades.SpringCriteriaAttributeLogic#handleGetMatchModeConstant()
155     */
156    protected String handleGetMatchModeConstant()
157    {
158        String matchMode = getMatchMode();
159        String matchModeConstant = null;
160
161        if (matchMode != null)
162        {
163            if (matchMode.equalsIgnoreCase(SpringProfile.TAGGEDVALUEVALUE_MATCHMODE_ANYWHERE))
164            {
165                matchModeConstant = "ANYWHERE";
166            }
167            else if (matchMode.equalsIgnoreCase(SpringProfile.TAGGEDVALUEVALUE_MATCHMODE_END))
168            {
169                matchModeConstant = "END";
170            }
171            else if (matchMode.equalsIgnoreCase(SpringProfile.TAGGEDVALUEVALUE_MATCHMODE_EXACT))
172            {
173                matchModeConstant = "EXACT";
174            }
175            else if (matchMode.equalsIgnoreCase(SpringProfile.TAGGEDVALUEVALUE_MATCHMODE_START))
176            {
177                matchModeConstant = "START";
178            }
179        }
180        return matchModeConstant;
181    }
182
183    /**
184     * @see org.andromda.cartridges.spring.metafacades.SpringCriteriaAttributeLogic#handleIsMatchModePresent()
185     */
186    protected boolean handleIsMatchModePresent()
187    {
188        return !StringUtils.isBlank(getMatchMode());
189    }
190
191    private static final String ORDER_UNSET = "ORDER_UNSET";
192
193    /**
194     * @see org.andromda.cartridges.spring.metafacades.SpringCriteriaAttributeLogic#handleIsOrderable()
195     */
196    protected boolean handleIsOrderable()
197    {
198        return !ORDER_UNSET.equals(getOrderDirection());
199    }
200
201    /**
202     * @see org.andromda.cartridges.spring.metafacades.SpringCriteriaAttributeLogic#handleGetOrderDirection()
203     */
204    protected String handleGetOrderDirection()
205    {
206        String result = ORDER_UNSET;
207        String value =
208            StringUtils.trimToEmpty(
209                (String)findTaggedValue(SpringProfile.TAGGEDVALUE_HIBERNATE_CRITERIA_ORDER_DIRECTION));
210        if (!StringUtils.isBlank(value))
211        {
212            if (value.equals(SpringProfile.TAGGEDVALUEVALUE_ORDER_ASCENDING))
213            {
214                result = "ORDER_ASC";
215            }
216            else if (value.equals(SpringProfile.TAGGEDVALUEVALUE_ORDER_DESCENDING))
217            {
218                result = "ORDER_DESC";
219            }
220        }
221        return result;
222    }
223
224    /**
225     * Used for undefined states of the criteria ordering.
226     */
227    private static final int UNSET = -1;
228
229    /**
230     * @see org.andromda.cartridges.spring.metafacades.SpringCriteriaAttributeLogic#handleGetOrderRelevance()
231     */
232    protected int handleGetOrderRelevance()
233    {
234        int result = UNSET;
235        String value =
236            StringUtils.trimToEmpty(
237                (String)findTaggedValue(SpringProfile.TAGGEDVALUE_HIBERNATE_CRITERIA_ORDER_RELEVANCE));
238        if (!StringUtils.isBlank(value))
239        {
240            try
241            {
242                result = Integer.parseInt(value);
243            }
244            catch (NumberFormatException ignore)
245            {
246            }
247        }
248        return result;
249    }
250
251    /**
252     * The default value for whether hibernate criteria arguments are case insensitive or not.
253     */
254    private static final String HIBERNATE_CRITERIA_QUERY_IGNORE_CASE = "hibernateCriteriaQueryIgnoreCase";
255
256    // TODO removed isIgnoreCase from MetafacadeModel, delete handleIsIgnoreCase? @see org.andromda.cartridges.spring.metafacades.SpringCriteriaAttributeLogic#isIgnoreCase()
257    /**
258     * @return findTaggedValue(SpringProfile.TAGGEDVALUE_HIBERNATE_CRITERIA_COMPARATOR_IGNORE_CASE)
259     */
260    protected boolean handleIsIgnoreCase()
261    {
262        Object value = this.findTaggedValue(SpringProfile.TAGGEDVALUE_HIBERNATE_CRITERIA_COMPARATOR_IGNORE_CASE);
263        if (value == null)
264        {
265            value = this.getConfiguredProperty(HIBERNATE_CRITERIA_QUERY_IGNORE_CASE);
266        }
267        return Boolean.valueOf(ObjectUtils.toString(value)).booleanValue();
268    }
269}