View Javadoc
1   /**
2    * This file Copyright (c) 2015 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.resources.app.action;
35  
36  import info.magnolia.commands.CommandsManager;
37  import info.magnolia.context.Context;
38  import info.magnolia.event.EventBus;
39  import info.magnolia.i18nsystem.SimpleTranslator;
40  import info.magnolia.jcr.util.NodeTypes;
41  import info.magnolia.resourceloader.jcr.JcrResourceOrigin;
42  import info.magnolia.resources.app.workbench.ResourcesContainer;
43  import info.magnolia.ui.api.app.SubAppContext;
44  import info.magnolia.ui.api.event.AdmincentralEventBus;
45  import info.magnolia.ui.framework.action.ActivationAction;
46  import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
47  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
48  
49  import java.util.Comparator;
50  import java.util.List;
51  
52  import javax.inject.Inject;
53  import javax.inject.Named;
54  import javax.jcr.Node;
55  import javax.jcr.RepositoryException;
56  
57  import org.apache.commons.lang3.StringUtils;
58  
59  import com.google.common.collect.Lists;
60  import com.google.common.collect.Ordering;
61  import com.vaadin.data.Item;
62  import com.vaadin.data.Property;
63  
64  /**
65   * UI action that allows to publish/un-publish a hotfixed version of a resource in resources app.
66   * <p>
67   * All the ancestor nodes have to be activated before the activation of a given node.
68   * Thus, initially this class activates ancestors of a given node.
69   * <p>
70   * Given item expected to have {@code ResourcesContainer#RESOURCES_WORKSPACE} property set.
71   */
72  public class HotfixActivationAction extends ActivationAction<HotfixActivationActionDefinition> {
73  
74      private final JcrItemAdapter jcrItemAdapter;
75      private final HotfixActivationActionDefinition definition;
76  
77      @Inject
78      public HotfixActivationAction(HotfixActivationActionDefinition definition,
79                                    Item item,
80                                    CommandsManager commandsManager,
81                                    @Named(AdmincentralEventBus.NAME) EventBus eventBus,
82                                    SubAppContext uiContext,
83                                    SimpleTranslator i18n,
84                                    Context context) {
85          this(definition, resolveJcrNodeAdapter(context, item), commandsManager, eventBus, uiContext, i18n);
86      }
87  
88      private HotfixActivationAction(HotfixActivationActionDefinition definition, JcrItemAdapter jcrItemAdapter, CommandsManager commandsManager,
89                                     EventBus eventBus, SubAppContext uiContext, SimpleTranslator i18n) {
90          super(definition, jcrItemAdapter, commandsManager, eventBus, uiContext, i18n);
91          this.definition = definition;
92          this.jcrItemAdapter = jcrItemAdapter;
93      }
94  
95      @Override
96      protected Comparator<JcrItemAdapter> getItemComparator() {
97          return new Comparator<JcrItemAdapter>() {
98              @Override
99              public int compare(JcrItemAdapter o1, JcrItemAdapter o2) {
100                 try {
101                     return o1.getJcrItem().getDepth() - o2.getJcrItem().getDepth();
102                 } catch (RepositoryException e) {
103                     return 0;
104                 }
105             }
106         };
107     }
108 
109     @Override
110     protected List<JcrItemAdapter> getItems() {
111         final List<JcrItemAdapter> items = Lists.newLinkedList();
112 
113         try {
114             if (!StringUtils.equals(definition.getCommand(), "deactivate")) {
115                 int depth = jcrItemAdapter.getJcrItem().getDepth();
116                 for (int i = 1; i < depth + 1; i++) {
117                     Node ancestor = (Node) jcrItemAdapter.getJcrItem().getAncestor(i);
118                     if (NodeTypes.Activatable.ACTIVATION_STATUS_ACTIVATED != NodeTypes.Activatable.getActivationStatus(ancestor)) {
119                         items.add(new JcrNodeAdapter(ancestor));
120                     }
121                 }
122             } else {
123                 items.add(new JcrNodeAdapter((Node) jcrItemAdapter.getJcrItem()));
124             }
125 
126             return items;
127         } catch (Exception e) {
128             throw new RuntimeException(e);
129         }
130     }
131 
132     @Override
133     protected List<JcrItemAdapter> getSortedItems(Comparator<JcrItemAdapter> comparator) {
134         return Ordering.from(comparator).sortedCopy(getItems());
135     }
136 
137     private static JcrItemAdapter resolveJcrNodeAdapter(Context context, Item item) {
138         try {
139             final Property resourcePathProperty = item.getItemProperty(ResourcesContainer.RESOURCE_PATH);
140             if (resourcePathProperty != null) {
141                 return new JcrNodeAdapter(context.getJCRSession(JcrResourceOrigin.RESOURCES_WORKSPACE).getNode(resourcePathProperty.getValue().toString()));
142             } else {
143                 throw new RepositoryException(String.format("Required [%s] property cannot be found", ResourcesContainer.RESOURCE_PATH));
144             }
145         } catch (RepositoryException e) {
146             throw new RuntimeException(e);
147         }
148     }
149 }