001    /**
002     * Copyright (c) 2000-present 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.portlet.dynamicdatamapping.storage;
016    
017    import com.liferay.portal.kernel.util.LocaleUtil;
018    import com.liferay.portal.kernel.util.Validator;
019    
020    import java.io.Serializable;
021    
022    import java.util.ArrayList;
023    import java.util.Collections;
024    import java.util.Comparator;
025    import java.util.HashMap;
026    import java.util.HashSet;
027    import java.util.Iterator;
028    import java.util.List;
029    import java.util.Locale;
030    import java.util.Map;
031    import java.util.Set;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class Fields implements Iterable<Field>, Serializable {
037    
038            public boolean contains(String name) {
039                    return _fieldsMap.containsKey(name);
040            }
041    
042            @Override
043            public boolean equals(Object obj) {
044                    return equals(obj, true);
045            }
046    
047            public boolean equals(Object obj, boolean includePrivateFields) {
048                    if (this == obj) {
049                            return true;
050                    }
051    
052                    if (!(obj instanceof Fields)) {
053                            return false;
054                    }
055    
056                    Fields fields = (Fields)obj;
057    
058                    if (includePrivateFields) {
059                            return Validator.equals(_fieldsMap, fields._fieldsMap);
060                    }
061    
062                    List<Field> fieldList1 = getFieldsList(includePrivateFields);
063                    List<Field> fieldList2 = fields.getFieldsList(includePrivateFields);
064    
065                    if (fieldList1.size() != fieldList2.size()) {
066                            return false;
067                    }
068    
069                    if (fieldList1.containsAll(fieldList2)) {
070                            return true;
071                    }
072    
073                    return false;
074            }
075    
076            public Field get(String name) {
077                    return _fieldsMap.get(name);
078            }
079    
080            public Set<Locale> getAvailableLocales() {
081                    Set<Locale> availableLocales = new HashSet<Locale>();
082    
083                    for (Field field : _fieldsMap.values()) {
084                            if (field.isPrivate()) {
085                                    continue;
086                            }
087    
088                            for (Locale availableLocale : field.getAvailableLocales()) {
089                                    availableLocales.add(availableLocale);
090                            }
091                    }
092    
093                    return availableLocales;
094            }
095    
096            public long getDDMStructureId() {
097                    long ddmStructureId = 0;
098    
099                    Iterator<Field> itr = iterator();
100    
101                    if (itr.hasNext()) {
102                            Field field = itr.next();
103    
104                            ddmStructureId = field.getDDMStructureId();
105                    }
106    
107                    return ddmStructureId;
108            }
109    
110            public Locale getDefaultLocale() {
111                    Locale defaultLocale = LocaleUtil.getSiteDefault();
112    
113                    Iterator<Field> itr = iterator();
114    
115                    if (itr.hasNext()) {
116                            Field field = itr.next();
117    
118                            defaultLocale = field.getDefaultLocale();
119                    }
120    
121                    return defaultLocale;
122            }
123    
124            public Set<String> getNames() {
125                    return _fieldsMap.keySet();
126            }
127    
128            @Override
129            public Iterator<Field> iterator() {
130                    return iterator(false);
131            }
132    
133            public Iterator<Field> iterator(boolean includePrivateFields) {
134                    return iterator(null, includePrivateFields);
135            }
136    
137            public Iterator<Field> iterator(
138                    Comparator<Field> comparator, boolean includePrivateFields) {
139    
140                    List<Field> fieldsList = getFieldsList(includePrivateFields);
141    
142                    if (comparator != null) {
143                            Collections.sort(fieldsList, comparator);
144                    }
145    
146                    return fieldsList.iterator();
147            }
148    
149            public void put(Field field) {
150                    _fieldsMap.put(field.getName(), field);
151            }
152    
153            public Field remove(String name) {
154                    return _fieldsMap.remove(name);
155            }
156    
157            protected List<Field> getFieldsList(boolean includePrivateFields) {
158                    List<Field> fieldsList = new ArrayList<Field>();
159    
160                    for (Field field : _fieldsMap.values()) {
161                            if (!includePrivateFields && field.isPrivate()) {
162                                    continue;
163                            }
164    
165                            fieldsList.add(field);
166                    }
167    
168                    return fieldsList;
169            }
170    
171            private final Map<String, Field> _fieldsMap = new HashMap<String, Field>();
172    
173    }