001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.tools.servicebuilder;
016    
017    import com.liferay.portal.kernel.util.StringBundler;
018    
019    import com.liferay.util.TextFormatter;
020    
021    import java.util.Iterator;
022    import java.util.List;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     * @author Connor McKay
027     */
028    public class EntityFinder {
029    
030            public EntityFinder(
031                    String name, String returnType, boolean unique, String where,
032                    boolean dbIndex, List<EntityColumn> columns) {
033    
034                    _name = name;
035                    _returnType = returnType;
036                    _unique = unique;
037                    _where = where;
038                    _dbIndex = dbIndex;
039                    _columns = columns;
040            }
041    
042            public List<EntityColumn> getColumns() {
043                    return _columns;
044            }
045    
046            public String getHumanConditions(boolean arrayable) {
047                    if (_columns.size() == 1) {
048                            return _columns.get(0).getHumanCondition(arrayable);
049                    }
050    
051                    Iterator<EntityColumn> itr = _columns.iterator();
052    
053                    StringBundler sb = new StringBundler();
054    
055                    while (itr.hasNext()) {
056                            EntityColumn column = itr.next();
057    
058                            sb.append(column.getHumanCondition(arrayable));
059    
060                            if (itr.hasNext()) {
061                                    sb.append(" and ");
062                            }
063                    }
064    
065                    return sb.toString();
066            }
067    
068            public String getName() {
069                    return _name;
070            }
071    
072            public String getNames() {
073                    return TextFormatter.formatPlural(_name);
074            }
075    
076            public String getReturnType() {
077                    return _returnType;
078            }
079    
080            public String getWhere() {
081                    return _where;
082            }
083    
084            public boolean hasArrayableOperator() {
085                    for (EntityColumn column : _columns) {
086                            if (column.hasArrayableOperator()) {
087                                    return true;
088                            }
089                    }
090    
091                    return false;
092            }
093    
094            public boolean hasColumn(String name) {
095                    return Entity.hasColumn(name, _columns);
096            }
097    
098            public boolean isCollection() {
099                    if ((_returnType != null) && _returnType.equals("Collection")) {
100                            return true;
101                    }
102                    else {
103                            return false;
104                    }
105            }
106    
107            public boolean isDBIndex() {
108                    return _dbIndex;
109            }
110    
111            public boolean isUnique() {
112                    return _unique;
113            }
114    
115            private List<EntityColumn> _columns;
116            private boolean _dbIndex;
117            private String _name;
118            private String _returnType;
119            private boolean _unique;
120            private String _where;
121    
122    }