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.StringPool;
019    import com.liferay.portal.kernel.util.TextFormatter;
020    
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 (name.equals(column.getName())) {
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                    StringBundler sb = new StringBundler(_columns.size() * 2);
061    
062                    for (EntityColumn column : _columns) {
063                            sb.append(column.getHumanCondition(arrayable));
064                            sb.append(" and ");
065                    }
066    
067                    if (!_columns.isEmpty()) {
068                            sb.setIndex(sb.index() - 1);
069                    }
070    
071                    return sb.toString();
072            }
073    
074            public String getName() {
075                    return _name;
076            }
077    
078            public String getNames() {
079                    return TextFormatter.formatPlural(_name);
080            }
081    
082            public String getReturnType() {
083                    return _returnType;
084            }
085    
086            public String getWhere() {
087                    return _where;
088            }
089    
090            public boolean hasArrayableOperator() {
091                    for (EntityColumn column : _columns) {
092                            if (column.hasArrayableOperator()) {
093                                    return true;
094                            }
095                    }
096    
097                    return false;
098            }
099    
100            public boolean hasColumn(String name) {
101                    return Entity.hasColumn(name, _columns);
102            }
103    
104            public boolean hasCustomComparator() {
105                    for (EntityColumn column : _columns) {
106                            String comparator = column.getComparator();
107    
108                            if (!comparator.equals(StringPool.EQUAL)) {
109                                    return true;
110                            }
111                    }
112    
113                    return false;
114            }
115    
116            public boolean isCollection() {
117                    if ((_returnType != null) && _returnType.equals("Collection")) {
118                            return true;
119                    }
120                    else {
121                            return false;
122                    }
123            }
124    
125            public boolean isDBIndex() {
126                    return _dbIndex;
127            }
128    
129            public boolean isUnique() {
130                    return _unique;
131            }
132    
133            private List<EntityColumn> _columns;
134            private boolean _dbIndex;
135            private String _name;
136            private String _returnType;
137            private boolean _unique;
138            private String _where;
139    
140    }