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.portlet.dynamicdatamapping.storage;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
021    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
022    
023    import java.io.Serializable;
024    
025    import java.util.Locale;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class Field implements Serializable {
031    
032            public Field() {
033            }
034    
035            public Field(long ddmStructureId, String name, Serializable value) {
036                    _ddmStructureId = ddmStructureId;
037                    _name = name;
038                    _value = value;
039            }
040    
041            public Field(String name, Serializable value) {
042                    this(0, name, value);
043            }
044    
045            @Override
046            public boolean equals(Object obj) {
047                    if (!(obj instanceof Field)) {
048                            return false;
049                    }
050    
051                    Field field = (Field)obj;
052    
053                    if ((_ddmStructureId == field._ddmStructureId) &&
054                            Validator.equals(_name, field._name) &&
055                            Validator.equals(_value, field._value)) {
056    
057                            return true;
058                    }
059    
060                    return false;
061            }
062    
063            public String getDataType() throws PortalException, SystemException {
064                    DDMStructure ddmStructure = getDDMStructure();
065    
066                    return ddmStructure.getFieldDataType(_name);
067            }
068    
069            public DDMStructure getDDMStructure() throws SystemException {
070                    return DDMStructureLocalServiceUtil.fetchStructure(_ddmStructureId);
071            }
072    
073            public long getDDMStructureId() {
074                    return _ddmStructureId;
075            }
076    
077            public String getName() {
078                    return _name;
079            }
080    
081            public String getRenderedValue(Locale locale)
082                    throws PortalException, SystemException {
083    
084                    DDMStructure ddmStructure = getDDMStructure();
085    
086                    String dataType = null;
087    
088                    if (ddmStructure != null) {
089                            dataType = getDataType();
090                    }
091    
092                    FieldRenderer fieldrenderer = FieldRendererFactory.getFieldRenderer(
093                            dataType);
094    
095                    return fieldrenderer.render(this, locale);
096            }
097    
098            public String getType() throws PortalException, SystemException {
099                    DDMStructure ddmStructure = getDDMStructure();
100    
101                    return ddmStructure.getFieldType(_name);
102            }
103    
104            public Serializable getValue() {
105                    return _value;
106            }
107    
108            public void setDDMStructureId(long ddmStructureId) {
109                    _ddmStructureId = ddmStructureId;
110            }
111    
112            public void setName(String name) {
113                    _name = name;
114            }
115    
116            public void setValue(Serializable value) {
117                    _value = value;
118            }
119    
120            private long _ddmStructureId;
121            private String _name;
122            private Serializable _value;
123    
124    }