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.ui.actions;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.file.CmsResourceFilter;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.relations.CmsRelation;
036import org.opencms.relations.CmsRelationFilter;
037import org.opencms.relations.CmsRelationType;
038import org.opencms.ui.I_CmsDialogContext;
039import org.opencms.ui.Messages;
040import org.opencms.ui.contextmenu.CmsMenuItemVisibilityMode;
041import org.opencms.ui.contextmenu.CmsMenuItemVisibilitySingleOnly;
042import org.opencms.ui.contextmenu.CmsStandardVisibilityCheck;
043import org.opencms.ui.contextmenu.I_CmsHasMenuItemVisibility;
044import org.opencms.ui.sitemap.CmsUnlinkDialog;
045
046import java.util.List;
047
048import org.apache.commons.logging.Log;
049
050/**
051 * Workplace action for the 'Link locale variant' dialog.<p>
052 */
053public class CmsUnlinkLocaleVariantAction extends A_CmsWorkplaceAction {
054
055    /** The action id. */
056    public static final String ACTION_ID = "unlinklocale";
057
058    /** The action visibility. */
059    public static final I_CmsHasMenuItemVisibility VISIBILITY = new CmsMenuItemVisibilitySingleOnly(
060        CmsStandardVisibilityCheck.DEFAULT_DEFAULTFILE);
061
062    /** Logger instance for this class. */
063    private static final Log LOG = CmsLog.getLog(CmsUnlinkLocaleVariantAction.class);
064
065    /**
066     * @see org.opencms.ui.actions.I_CmsWorkplaceAction#executeAction(org.opencms.ui.I_CmsDialogContext)
067     */
068    public void executeAction(final I_CmsDialogContext context) {
069
070        try {
071            final CmsResource resource = context.getResources().get(0);
072            CmsObject cms = context.getCms();
073            List<CmsRelation> relations = readOutgoingRelations(cms, resource);
074            for (CmsRelation relation : relations) {
075                try {
076                    CmsResource target = relation.getTarget(cms, CmsResourceFilter.IGNORE_EXPIRATION);
077                    CmsUnlinkDialog unlinkDialog = new CmsUnlinkDialog(context, target);
078                    openDialog(unlinkDialog, context);
079                    break;
080                } catch (CmsException e) {
081                    LOG.info("No target found for: " + relation, e);
082                }
083            }
084        } catch (Exception e) {
085            LOG.error(e.getLocalizedMessage(), e);
086            context.error(e);
087        }
088    }
089
090    /**
091     * @see org.opencms.ui.actions.I_CmsWorkplaceAction#getId()
092     */
093    public String getId() {
094
095        return ACTION_ID;
096    }
097
098    /**
099     * @see org.opencms.ui.actions.A_CmsWorkplaceAction#getTitleKey()
100     */
101    @Override
102    public String getTitleKey() {
103
104        return Messages.GUI_LOCALECOMPARE_UNLINK_LOCALE_VARIANT_0;
105    }
106
107    /**
108     * @see org.opencms.ui.contextmenu.I_CmsHasMenuItemVisibility#getVisibility(org.opencms.file.CmsObject, java.util.List)
109     */
110    public CmsMenuItemVisibilityMode getVisibility(CmsObject cms, List<CmsResource> resources) {
111
112        CmsMenuItemVisibilityMode visibility = VISIBILITY.getVisibility(cms, resources);
113        if (visibility.isInVisible() || visibility.isInActive()) {
114            return visibility;
115        }
116        try {
117            List<CmsRelation> relations = readOutgoingRelations(cms, resources.get(0));
118            boolean hasRelations = false;
119            for (CmsRelation relation : relations) {
120                if (!relation.getTargetId().isNullUUID()) {
121                    hasRelations = true;
122                    break;
123                }
124            }
125            return hasRelations
126            ? CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE
127            : CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
128        } catch (CmsException e) {
129            LOG.error(e.getLocalizedMessage(), e);
130            return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
131        }
132    }
133
134    /**
135     * Reads the outgoing LOCALE_VARIANT relations for a given resource.<p>
136     *
137     * @param cms the CMS context
138     * @param resource the resource
139     * @return the list of relations
140     * @throws CmsException if something goes wrong
141     */
142    List<CmsRelation> readOutgoingRelations(CmsObject cms, CmsResource resource) throws CmsException {
143
144        List<CmsRelation> results = cms.readRelations(
145            CmsRelationFilter.relationsFromStructureId(resource.getStructureId()).filterType(
146                CmsRelationType.LOCALE_VARIANT));
147        return results;
148    }
149
150}