001
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
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 if (isCollection() && isUnique() && !hasArrayableOperator()) {
041 throw new IllegalArgumentException(
042 "A finder cannot return a Collection and be unique unless " +
043 "it has an arrayable column. See the ExpandoColumn " +
044 "service.xml declaration for an example.");
045 }
046
047 if ((!isCollection() || isUnique()) && hasCustomComparator()) {
048 throw new IllegalArgumentException(
049 "A unique finder cannot have a custom comparator");
050 }
051 }
052
053 public EntityColumn getColumn(String name) {
054 for (EntityColumn column : _columns) {
055 if (name.equals(column.getName())) {
056 return column;
057 }
058 }
059
060 return null;
061 }
062
063 public List<EntityColumn> getColumns() {
064 return _columns;
065 }
066
067 public String getHumanConditions(boolean arrayable) {
068 if (_columns.size() == 1) {
069 return _columns.get(0).getHumanCondition(arrayable);
070 }
071
072 StringBundler sb = new StringBundler(_columns.size() * 2);
073
074 for (EntityColumn column : _columns) {
075 sb.append(column.getHumanCondition(arrayable));
076 sb.append(" and ");
077 }
078
079 if (!_columns.isEmpty()) {
080 sb.setIndex(sb.index() - 1);
081 }
082
083 return sb.toString();
084 }
085
086 public String getName() {
087 return _name;
088 }
089
090 public String getNames() {
091 return TextFormatter.formatPlural(_name);
092 }
093
094 public String getReturnType() {
095 return _returnType;
096 }
097
098 public String getWhere() {
099 return _where;
100 }
101
102 public boolean hasArrayableOperator() {
103 for (EntityColumn column : _columns) {
104 if (column.hasArrayableOperator()) {
105 return true;
106 }
107 }
108
109 return false;
110 }
111
112 public boolean hasColumn(String name) {
113 return Entity.hasColumn(name, _columns);
114 }
115
116 public boolean hasCustomComparator() {
117 for (EntityColumn column : _columns) {
118 String comparator = column.getComparator();
119
120 if (!comparator.equals(StringPool.EQUAL)) {
121 return true;
122 }
123 }
124
125 return false;
126 }
127
128 public boolean isCollection() {
129 if ((_returnType != null) && _returnType.equals("Collection")) {
130 return true;
131 }
132 else {
133 return false;
134 }
135 }
136
137 public boolean isDBIndex() {
138 return _dbIndex;
139 }
140
141 public boolean isUnique() {
142 return _unique;
143 }
144
145 private List<EntityColumn> _columns;
146 private boolean _dbIndex;
147 private String _name;
148 private String _returnType;
149 private boolean _unique;
150 private String _where;
151
152 }