View Javadoc
1   /**
2    * This file Copyright (c) 2015-2017 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.jcr.util.NodeUtil;
42  import info.magnolia.resources.app.utils.ResourceUtils;
43  import info.magnolia.ui.api.app.SubAppContext;
44  import info.magnolia.ui.api.event.AdmincentralEventBus;
45  import info.magnolia.ui.api.event.ContentChangedEvent;
46  import info.magnolia.ui.framework.action.ActivationAction;
47  import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
48  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
49  
50  import java.util.ArrayList;
51  import java.util.Comparator;
52  import java.util.List;
53  
54  import javax.inject.Named;
55  import javax.jcr.Node;
56  import javax.jcr.RepositoryException;
57  
58  import org.apache.commons.lang3.StringUtils;
59  import org.slf4j.Logger;
60  import org.slf4j.LoggerFactory;
61  
62  import com.google.common.collect.Lists;
63  import com.google.common.collect.Ordering;
64  import com.vaadin.data.Item;
65  
66  /**
67   * UI action that allows to publish/un-publish a resource in resources app.
68   * <p>
69   * All the ancestor nodes have to be activated before the activation of a given node.
70   * Thus, initially this class activates ancestors of a given node.
71   * <p>
72   * Given item expected to have {@code ResourcesContainer#RESOURCES_WORKSPACE} property set.
73   */
74  public class ResourceActivationAction extends ActivationAction<ResourceActivationActionDefinition> {
75  
76      private static final Logger log = LoggerFactory.getLogger(ResourceActivationAction.class);
77  
78      private final ResourceActivationActionDefinition definition;
79      private final EventBus eventBus;
80      private final List<String> changedItemPaths = new ArrayList<>();
81  
82      public ResourceActivationAction(ResourceActivationActionDefinition definition, Item item, CommandsManager commandsManager, @Named(AdmincentralEventBus.NAME) EventBus eventBus, SubAppContext uiContext, SimpleTranslator i18n, Context context) {
83          this(definition, Lists.newArrayList(item), commandsManager, eventBus, uiContext, i18n, context);
84      }
85  
86      public ResourceActivationAction(ResourceActivationActionDefinition definition, List<Item> items, CommandsManager commandsManager, @Named(AdmincentralEventBus.NAME) EventBus eventBus, SubAppContext uiContext, SimpleTranslator i18n, Context context) {
87          this(definition, ResourceUtils.getJcrItemAdaptersFor(context, items), commandsManager, eventBus, uiContext, i18n);
88      }
89  
90      private ResourceActivationAction(ResourceActivationActionDefinition definition, List<JcrItemAdapter> items, CommandsManager commandsManager, @Named(AdmincentralEventBus.NAME) EventBus eventBus, SubAppContext uiContext, SimpleTranslator i18n) {
91          super(definition, items, commandsManager, eventBus, uiContext, i18n);
92          this.definition = definition;
93          this.eventBus = eventBus;
94          for (JcrItemAdapter itemAdapter : items) {
95              try {
96                  changedItemPaths.add(itemAdapter.getJcrItem().getPath());
97              } catch (RepositoryException e) {
98                  log.warn("Unable to obtain path for item {}", itemAdapter.getItemId());
99              }
100         }
101     }
102 
103     @Override
104     protected Comparator<JcrItemAdapter> getItemComparator() {
105         return new Comparator<JcrItemAdapter>() {
106             @Override
107             public int compare(JcrItemAdapter o1, JcrItemAdapter o2) {
108                 try {
109                     if (getDefinition().isDeletionActivation()) {
110                         return o2.getJcrItem().getDepth() - o1.getJcrItem().getDepth();
111                     }
112                     return o1.getJcrItem().getDepth() - o2.getJcrItem().getDepth();
113                 } catch (RepositoryException e) {
114                     return 0;
115                 }
116             }
117         };
118     }
119 
120     @Override
121     protected List<JcrItemAdapter> getItems() {
122         final List<JcrItemAdapter> allItems = Lists.newLinkedList();
123 
124         for (JcrItemAdapter resourceItem : super.getItems()) {
125             allItems.addAll(resolveJcrItemsToPublish((JcrNodeAdapter) resourceItem));
126         }
127         return allItems;
128     }
129 
130     private List<JcrItemAdapter> resolveJcrItemsToPublish(JcrNodeAdapter jcrItemAdapter) {
131         List<JcrItemAdapter> items = Lists.newLinkedList();
132         try {
133             if (!StringUtils.equals(definition.getCommand(), "deactivate") || !definition.isRecursive()) {
134                 int depth = jcrItemAdapter.getJcrItem().getDepth();
135                 for (int i = 1; i < depth + 1; i++) {
136                     Node ancestor = (Node) jcrItemAdapter.getJcrItem().getAncestor(i);
137                     if (!definition.isDeletionActivation() && isNodeNotActivated(ancestor)) {
138                         items.add(new JcrNodeAdapter(ancestor));
139                     } else if (definition.isDeletionActivation() && isNodeDeleted(ancestor)) {
140                         items.add(new JcrNodeAdapter(ancestor));
141                     }
142                 }
143             }
144         } catch (RepositoryException e) {
145             log.error("Resource cannot be resolved", e);
146         }
147 
148         // This block used for simple activation operation, as well as recursive activation and deactivate requests.
149         if (items.isEmpty()) {
150             items.add(new JcrNodeAdapter(jcrItemAdapter.getJcrItem()));
151         }
152 
153         return items;
154     }
155 
156     /**
157      * Returns {@code true}, If node is deleted.
158      */
159     private boolean isNodeDeleted(Node ancestor) {
160         try {
161             return NodeUtil.hasMixin(ancestor, NodeTypes.Deleted.NAME);
162         } catch (RepositoryException e) {
163             log.error("Node is not being found.", e);
164             return false;
165         }
166     }
167 
168     /**
169      * Returns {@code true}, If node is not activated.
170      */
171     private boolean isNodeNotActivated(Node node) {
172         try {
173             return !NodeTypes.Activatable.isActivated(node);
174         } catch (RepositoryException e) {
175             log.error("Node is not being found.", e);
176             return false;
177         }
178     }
179 
180     @Override
181     protected List<JcrItemAdapter> getSortedItems(Comparator<JcrItemAdapter> comparator) {
182         return Ordering.from(comparator).sortedCopy(getItems());
183     }
184 
185     // Refreshing the resources container after activation action.
186     // Since this action might activate deletion as well, thus, in that case we should be activating the parent node for reflecting changes to UI.
187     @Override
188     protected void onPostExecute() throws Exception {
189         super.onPostExecute();
190         eventBus.fireEvent(new ContentChangedEvent(changedItemPaths));
191     }
192 }