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 GmbH & Co. KG, 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.workplace.tools.link;
029
030import org.opencms.file.CmsResource;
031import org.opencms.jsp.CmsJspActionElement;
032import org.opencms.main.CmsException;
033import org.opencms.main.OpenCms;
034import org.opencms.util.CmsFileUtil;
035import org.opencms.util.CmsStringUtil;
036import org.opencms.widgets.CmsVfsFileWidget;
037import org.opencms.workplace.CmsDialog;
038import org.opencms.workplace.CmsWidgetDialog;
039import org.opencms.workplace.CmsWidgetDialogParameter;
040import org.opencms.workplace.CmsWorkplaceSettings;
041
042import java.util.ArrayList;
043import java.util.Iterator;
044import java.util.List;
045import java.util.Map;
046
047import javax.servlet.http.HttpServletRequest;
048import javax.servlet.http.HttpServletResponse;
049import javax.servlet.jsp.PageContext;
050
051/**
052 * Dialog in the administration view, to edit the resources to check the internal links for.<p>
053 *
054 * @since 6.5.3
055 */
056public class CmsInternalLinkValidationDialog extends CmsWidgetDialog {
057
058    /** localized messages Keys prefix. */
059    public static final String KEY_PREFIX = "internallinks";
060
061    /** Defines which pages are valid for this dialog. */
062    public static final String[] PAGES = {"page1"};
063
064    /** Auxiliary Property for the VFS resources. */
065    private List m_resources;
066
067    /**
068     * Public constructor with JSP action element.<p>
069     *
070     * @param jsp an initialized JSP action element
071     */
072    public CmsInternalLinkValidationDialog(CmsJspActionElement jsp) {
073
074        super(jsp);
075    }
076
077    /**
078     * Public constructor with JSP variables.<p>
079     *
080     * @param context the JSP page context
081     * @param req the JSP request
082     * @param res the JSP response
083     */
084    public CmsInternalLinkValidationDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
085
086        this(new CmsJspActionElement(context, req, res));
087    }
088
089    /**
090     * Commits the edited project to the db.<p>
091     */
092    @Override
093    public void actionCommit() {
094
095        List errors = new ArrayList();
096
097        try {
098            setDialogObject(m_resources);
099            // refresh the list
100            Map objects = (Map)getSettings().getListObject();
101            if (objects != null) {
102                objects.remove(CmsInternalLinkValidationList.class.getName());
103            }
104            // forward to the list
105            getToolManager().jspForwardTool(this, "/linkvalidation/internallinks/list", null);
106        } catch (Throwable t) {
107            errors.add(t);
108        }
109        // set the list of errors to display when saving failed
110        setCommitErrors(errors);
111    }
112
113    /**
114     * Returns the list of VFS resources.<p>
115     *
116     * @return the list of VFS resources
117     */
118    public List getResources() {
119
120        if ((m_resources == null) || m_resources.isEmpty()) {
121            m_resources = new ArrayList();
122            m_resources.add("/");
123        }
124        return m_resources;
125    }
126
127    /**
128     * Sets the resources list.<p>
129     *
130     * @param value the resources to set
131     */
132    public void setResources(List value) {
133
134        if (value == null) {
135            m_resources = new ArrayList();
136            return;
137        }
138        m_resources = CmsFileUtil.removeRedundancies(value);
139    }
140
141    /**
142     * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String)
143     */
144    @Override
145    protected String createDialogHtml(String dialog) {
146
147        StringBuffer result = new StringBuffer(1024);
148
149        result.append(createWidgetTableStart());
150        // show error header once if there were validation errors
151        result.append(createWidgetErrorHeader());
152
153        if (dialog.equals(PAGES[0])) {
154            // create the widgets for the first dialog page
155            result.append(dialogBlockStart(key(Messages.GUI_INTERNALLINK_EDITOR_LABEL_BLOCK_0)));
156            result.append(createWidgetTableStart());
157            result.append(createDialogRowsHtml(0, 0));
158            result.append(createWidgetTableEnd());
159            result.append(dialogBlockEnd());
160        }
161
162        result.append(createWidgetTableEnd());
163        return result.toString();
164    }
165
166    /**
167     * Creates the list of widgets for this dialog.<p>
168     */
169    @Override
170    protected void defineWidgets() {
171
172        // initialize the project object to use for the dialog
173        initSessionObject();
174
175        setKeyPrefix(KEY_PREFIX);
176
177        // widgets to display
178        addWidget(new CmsWidgetDialogParameter(
179            this,
180            "resources",
181            "/",
182            PAGES[0],
183            new CmsVfsFileWidget(false, null),
184            1,
185            CmsWidgetDialogParameter.MAX_OCCURENCES));
186    }
187
188    /**
189     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
190     */
191    @Override
192    protected String[] getPageArray() {
193
194        return PAGES;
195    }
196
197    /**
198     * @see org.opencms.workplace.CmsWorkplace#initMessages()
199     */
200    @Override
201    protected void initMessages() {
202
203        // add specific dialog resource bundle
204        addMessages(Messages.get().getBundleName());
205        // add default resource bundles
206        super.initMessages();
207    }
208
209    /**
210     * Initializes the session object to work with depending on the dialog state and request parameters.<p>
211     */
212    protected void initSessionObject() {
213
214        try {
215            if (!CmsStringUtil.isEmpty(getParamAction()) && !CmsDialog.DIALOG_INITIAL.equals(getParamAction())) {
216                m_resources = (List)getDialogObject();
217                // test
218                m_resources.size();
219            }
220        } catch (Exception e) {
221            // ignore
222        }
223        if (m_resources == null) {
224            // create a new project object
225            m_resources = new ArrayList();
226            try {
227                Iterator it = OpenCms.getOrgUnitManager().getResourcesForOrganizationalUnit(
228                    getCms(),
229                    getCms().getRequestContext().getOuFqn()).iterator();
230                while (it.hasNext()) {
231                    CmsResource res = (CmsResource)it.next();
232                    m_resources.add(getCms().getSitePath(res));
233                }
234            } catch (CmsException e1) {
235                // should never happen
236            }
237        }
238    }
239
240    /**
241     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
242     */
243    @Override
244    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
245
246        // initialize parameters and dialog actions in super implementation
247        super.initWorkplaceRequestValues(settings, request);
248
249        // save the current state of the project (may be changed because of the widget values)
250        setDialogObject(m_resources);
251    }
252}