001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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
076        m_entity = entity;
077        m_handler = handler;
078        m_index = index;
079    }
080
081    /**
082     * Gets the id of this accessor.<p>
083     *
084     * @return the id
085     */
086    public String getId() {
087
088        return m_id;
089    }
090
091    /**
092     * Gets the current value in the editor.<p>
093     *
094     * @return the value in the editor
095     */
096    public CmsDataViewValue getValue() {
097
098        return new CmsDataViewValue(
099            getString(CmsDataViewConstants.VALUE_ID),
100            getString(CmsDataViewConstants.VALUE_TITLE),
101            getString(CmsDataViewConstants.VALUE_DESCRIPTION),
102            getString(CmsDataViewConstants.VALUE_DATA));
103
104    }
105
106    /**
107     * Replaces the value in the editor with a list of other values.<p>
108     *
109     * @param replacementValues the list of replacement values
110     */
111    public void replaceValue(List<CmsDataViewValue> replacementValues) {
112
113        CmsAttributeHandler handler = (CmsAttributeHandler)m_handler;
114        Element parent = CmsDomUtil.getAncestor(
115            m_widget.getElement(),
116            I_CmsLayoutBundle.INSTANCE.form().attributeValue()).getParentElement();
117        NodeList<Node> siblings = parent.getChildNodes();
118        for (int j = 0; j < siblings.getLength(); j++) {
119            Node node = siblings.getItem(j);
120            if (node instanceof Element) {
121                Element elem = (Element)node;
122                if (elem.isOrHasChild(m_widget.getElement())) {
123                    m_index = j;
124                    break;
125                }
126            }
127        }
128        Panel container = handler.removeAttributeValueAndReturnPrevParent(m_index, true);
129        int i = m_index;
130        for (CmsDataViewValue value : replacementValues) {
131            CmsEntity entity = CmsEntityBackend.getInstance().createEntity(null, m_entity.getTypeName());
132
133            writeValueToEntity(value, entity);
134
135            // handler.addNewAttributeValue(entity);
136            handler.insertNewAttributeValue(entity, i, container);
137            i += 1;
138        }
139
140        handler.updateButtonVisisbility();
141    }
142
143    /**
144     * Sets the widget instance.<p>
145     *
146     * @param cmsDataViewClientWidget the widget instance
147     */
148    public void setWidget(CmsDataViewClientWidget cmsDataViewClientWidget) {
149
150        m_widget = cmsDataViewClientWidget;
151    }
152
153    /**
154     * Gets a string-valued attribute from this accessor's entity.<p>
155     *
156     * @param valueKey the key
157     * @return the attribute value
158     */
159    private String getString(String valueKey) {
160
161        String realKey = m_entity.getTypeName() + "/" + valueKey;
162        CmsEntityAttribute attr = m_entity.getAttribute(realKey);
163        if (attr == null) {
164            return "";
165        } else {
166            return attr.getSimpleValue();
167        }
168    }
169
170    /**
171     * Writes the value to the given entity.<p>
172     *
173     * @param val the value
174     * @param entity the entity
175     */
176    private void writeValueToEntity(CmsDataViewValue val, CmsEntity entity) {
177
178        String prefix = entity.getTypeName() + "/";
179        entity.setAttributeValue(prefix + CmsDataViewConstants.VALUE_ID, val.getId());
180        entity.setAttributeValue(prefix + CmsDataViewConstants.VALUE_TITLE, val.getTitle());
181        entity.setAttributeValue(prefix + CmsDataViewConstants.VALUE_DESCRIPTION, val.getDescription());
182        entity.setAttributeValue(prefix + CmsDataViewConstants.VALUE_DATA, val.getData());
183    }
184
185}