001    /**
002     * Copyright (c) 2000-2013 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 (this == obj) {
048                            return true;
049                    }
050    
051                    if (!(obj instanceof Field)) {
052                            return false;
053                    }
054    
055                    Field field = (Field)obj;
056    
057                    if ((_ddmStructureId == field._ddmStructureId) &&
058                            Validator.equals(_name, field._name) &&
059                            Validator.equals(_value, field._value)) {
060    
061                            return true;
062                    }
063    
064                    return false;
065            }
066    
067            public String getDataType() throws PortalException, SystemException {
068                    DDMStructure ddmStructure = getDDMStructure();
069    
070                    return ddmStructure.getFieldDataType(_name);
071            }
072    
073            public DDMStructure getDDMStructure() throws SystemException {
074                    return DDMStructureLocalServiceUtil.fetchStructure(_ddmStructureId);
075            }
076    
077            public long getDDMStructureId() {
078                    return _ddmStructureId;
079            }
080    
081            public String getName() {
082                    return _name;
083            }
084    
085            public String getRenderedValue(Locale locale)
086                    throws PortalException, SystemException {
087    
088                    DDMStructure ddmStructure = getDDMStructure();
089    
090                    String dataType = null;
091    
092                    if (ddmStructure != null) {
093                            dataType = getDataType();
094                    }
095    
096                    FieldRenderer fieldrenderer = FieldRendererFactory.getFieldRenderer(
097                            dataType);
098    
099                    return fieldrenderer.render(this, locale);
100            }
101    
102            public String getType() throws PortalException, SystemException {
103                    DDMStructure ddmStructure = getDDMStructure();
104    
105                    return ddmStructure.getFieldType(_name);
106            }
107    
108            public Serializable getValue() {
109                    return _value;
110            }
111    
112            public void setDDMStructureId(long ddmStructureId) {
113                    _ddmStructureId = ddmStructureId;
114            }
115    
116            public void setName(String name) {
117                    _name = name;
118            }
119    
120            public void setValue(Serializable value) {
121                    _value = value;
122            }
123    
124            private long _ddmStructureId;
125            private String _name;
126            private Serializable _value;
127    
128    }