001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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 EntityColumn getColumn(String name) {
043                    for (EntityColumn column : _columns) {
044                            if (column.getName().equals(name)) {
045                                    return column;
046                            }
047                    }
048    
049                    return null;
050            }
051    
052            public List<EntityColumn> getColumns() {
053                    return _columns;
054            }
055    
056            public String getHumanConditions(boolean arrayable) {
057                    if (_columns.size() == 1) {
058                            return _columns.get(0).getHumanCondition(arrayable);
059                    }
060    
061                    Iterator<EntityColumn> itr = _columns.iterator();
062    
063                    StringBundler sb = new StringBundler();
064    
065                    while (itr.hasNext()) {
066                            EntityColumn column = itr.next();
067    
068                            sb.append(column.getHumanCondition(arrayable));
069    
070                            if (itr.hasNext()) {
071                                    sb.append(" and ");
072                            }
073                    }
074    
075                    return sb.toString();
076            }
077    
078            public String getName() {
079                    return _name;
080            }
081    
082            public String getNames() {
083                    return TextFormatter.formatPlural(_name);
084            }
085    
086            public String getReturnType() {
087                    return _returnType;
088            }
089    
090            public String getWhere() {
091                    return _where;
092            }
093    
094            public boolean hasArrayableOperator() {
095                    for (EntityColumn column : _columns) {
096                            if (column.hasArrayableOperator()) {
097                                    return true;
098                            }
099                    }
100    
101                    return false;
102            }
103    
104            public boolean hasColumn(String name) {
105                    return Entity.hasColumn(name, _columns);
106            }
107    
108            public boolean isCollection() {
109                    if ((_returnType != null) && _returnType.equals("Collection")) {
110                            return true;
111                    }
112                    else {
113                            return false;
114                    }
115            }
116    
117            public boolean isDBIndex() {
118                    return _dbIndex;
119            }
120    
121            public boolean isUnique() {
122                    return _unique;
123            }
124    
125            private List<EntityColumn> _columns;
126            private boolean _dbIndex;
127            private String _name;
128            private String _returnType;
129            private boolean _unique;
130            private String _where;
131    
132    }