001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.acacia.client.widgets.complex;
029
030import org.opencms.acacia.client.CmsAttributeHandler;
031import org.opencms.acacia.client.I_CmsAttributeHandler;
032import org.opencms.acacia.client.css.I_CmsLayoutBundle;
033import org.opencms.acacia.client.entity.CmsEntityBackend;
034import org.opencms.acacia.shared.CmsEntity;
035import org.opencms.acacia.shared.CmsEntityAttribute;
036import org.opencms.gwt.client.util.CmsDomUtil;
037import org.opencms.gwt.shared.CmsDataViewConstants;
038
039import java.util.List;
040
041import com.google.gwt.dom.client.Element;
042import com.google.gwt.dom.client.Node;
043import com.google.gwt.dom.client.NodeList;
044import com.google.gwt.user.client.ui.Panel;
045import com.google.gwt.user.client.ui.Widget;
046
047/**
048 * Class responsible for reading data view values from the editor, and writing them back to the editor.<p>
049 */
050public class CmsDataViewValueAccessor {
051
052    /** The id of this accessor. */
053    private String m_id = "" + Math.random();
054
055    /** Entity which the data view widget is currently displaying. */
056    private CmsEntity m_entity;
057
058    /** The attribute handler. */
059    private I_CmsAttributeHandler m_handler;
060
061    /** The value index. */
062    private int m_index;
063
064    /** The widget instance. */
065    private Widget m_widget;
066
067    /**
068     * Creates a new instance.<p>
069     *
070     * @param entity the entity
071     * @param handler the attribute handler
072     * @param index the value index
073     */
074    public CmsDataViewValueAccessor(CmsEntity entity, I_CmsAttributeHandler handler, int index) {
075        m_entity = entity;
076        m_handler = handler;
077        m_index = index;
078    }
079
080    /**
081     * Gets the id of this accessor.<p>
082     *
083     * @return the id
084     */
085    public String getId() {
086
087        return m_id;
088    }
089
090    /**
091     * Gets the current value in the editor.<p>
092     *
093     * @return the value in the editor
094     */
095    public CmsDataViewValue getValue() {
096
097        return new CmsDataViewValue(
098            getString(CmsDataViewConstants.VALUE_ID),
099            getString(CmsDataViewConstants.VALUE_TITLE),
100            getString(CmsDataViewConstants.VALUE_DESCRIPTION),
101            getString(CmsDataViewConstants.VALUE_DATA));
102
103    }
104
105    /**
106     * Replaces the value in the editor with a list of other values.<p>
107     *
108     * @param replacementValues the list of replacement values
109     */
110    public void replaceValue(List<CmsDataViewValue> replacementValues) {
111
112        CmsAttributeHandler handler = (CmsAttributeHandler)m_handler;
113        Element parent = CmsDomUtil.getAncestor(
114            m_widget.getElement(),
115            I_CmsLayoutBundle.INSTANCE.form().attributeValue()).getParentElement();
116        NodeList<Node> siblings = parent.getChildNodes();
117        for (int j = 0; j < siblings.getLength(); j++) {
118            Node node = siblings.getItem(j);
119            if (node instanceof Element) {
120                Element elem = (Element)node;
121                if (elem.isOrHasChild(m_widget.getElement())) {
122                    m_index = j;
123                    break;
124                }
125            }
126        }
127        Panel container = handler.removeAttributeValueAndReturnPrevParent(m_index, true);
128        int i = m_index;
129        for (CmsDataViewValue value : replacementValues) {
130            CmsEntity entity = CmsEntityBackend.getInstance().createEntity(null, m_entity.getTypeName());
131
132            writeValueToEntity(value, entity);
133
134            // handler.addNewAttributeValue(entity);
135            handler.insertNewAttributeValue(entity, i, container);
136            i += 1;
137        }
138
139        handler.updateButtonVisisbility();
140    }
141
142    /**
143     * Sets the widget instance.<p>
144     *
145     * @param cmsDataViewClientWidget the widget instance
146     */
147    public void setWidget(CmsDataViewClientWidget cmsDataViewClientWidget) {
148
149        m_widget = cmsDataViewClientWidget;
150    }
151
152    /**
153     * Gets a string-valued attribute from this accessor's entity.<p>
154     *
155     * @param valueKey the key
156     * @return the attribute value
157     */
158    private String getString(String valueKey) {
159
160        String realKey = m_entity.getTypeName() + "/" + valueKey;
161        CmsEntityAttribute attr = m_entity.getAttribute(realKey);
162        if (attr == null) {
163            return "";
164        } else {
165            return attr.getSimpleValue();
166        }
167    }
168
169    /**
170     * Writes the value to the given entity.<p>
171     *
172     * @param val the value
173     * @param entity the entity
174     */
175    private void writeValueToEntity(CmsDataViewValue val, CmsEntity entity) {
176
177        String prefix = entity.getTypeName() + "/";
178        entity.setAttributeValue(prefix + CmsDataViewConstants.VALUE_ID, val.getId());
179        entity.setAttributeValue(prefix + CmsDataViewConstants.VALUE_TITLE, val.getTitle());
180        entity.setAttributeValue(prefix + CmsDataViewConstants.VALUE_DESCRIPTION, val.getDescription());
181        entity.setAttributeValue(prefix + CmsDataViewConstants.VALUE_DATA, val.getData());
182    }
183
184}