001    /**
002     * Copyright (c) 2000-2012 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    import com.liferay.portal.kernel.util.TextFormatter;
019    
020    import java.util.Iterator;
021    import java.util.List;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     * @author Connor McKay
026     */
027    public class EntityFinder {
028    
029            public EntityFinder(
030                    String name, String returnType, boolean unique, String where,
031                    boolean dbIndex, List<EntityColumn> columns) {
032    
033                    _name = name;
034                    _returnType = returnType;
035                    _unique = unique;
036                    _where = where;
037                    _dbIndex = dbIndex;
038                    _columns = columns;
039            }
040    
041            public EntityColumn getColumn(String name) {
042                    for (EntityColumn column : _columns) {
043                            if (column.getName().equals(name)) {
044                                    return column;
045                            }
046                    }
047    
048                    return null;
049            }
050    
051            public List<EntityColumn> getColumns() {
052                    return _columns;
053            }
054    
055            public String getHumanConditions(boolean arrayable) {
056                    if (_columns.size() == 1) {
057                            return _columns.get(0).getHumanCondition(arrayable);
058                    }
059    
060                    Iterator<EntityColumn> itr = _columns.iterator();
061    
062                    StringBundler sb = new StringBundler();
063    
064                    while (itr.hasNext()) {
065                            EntityColumn column = itr.next();
066    
067                            sb.append(column.getHumanCondition(arrayable));
068    
069                            if (itr.hasNext()) {
070                                    sb.append(" and ");
071                            }
072                    }
073    
074                    return sb.toString();
075            }
076    
077            public String getName() {
078                    return _name;
079            }
080    
081            public String getNames() {
082                    return TextFormatter.formatPlural(_name);
083            }
084    
085            public String getReturnType() {
086                    return _returnType;
087            }
088    
089            public String getWhere() {
090                    return _where;
091            }
092    
093            public boolean hasArrayableOperator() {
094                    for (EntityColumn column : _columns) {
095                            if (column.hasArrayableOperator()) {
096                                    return true;
097                            }
098                    }
099    
100                    return false;
101            }
102    
103            public boolean hasColumn(String name) {
104                    return Entity.hasColumn(name, _columns);
105            }
106    
107            public boolean isCollection() {
108                    if ((_returnType != null) && _returnType.equals("Collection")) {
109                            return true;
110                    }
111                    else {
112                            return false;
113                    }
114            }
115    
116            public boolean isDBIndex() {
117                    return _dbIndex;
118            }
119    
120            public boolean isUnique() {
121                    return _unique;
122            }
123    
124            private List<EntityColumn> _columns;
125            private boolean _dbIndex;
126            private String _name;
127            private String _returnType;
128            private boolean _unique;
129            private String _where;
130    
131    }