View Javadoc
1   /**
2    * This file Copyright (c) 2015-2018 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.v7.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 (o1, o2) -> {
106             try {
107                 if (getDefinition().isDeletionActivation()) {
108                     return o2.getJcrItem().getDepth() - o1.getJcrItem().getDepth();
109                 }
110                 return o1.getJcrItem().getDepth() - o2.getJcrItem().getDepth();
111             } catch (RepositoryException e) {
112                 return 0;
113             }
114         };
115     }
116 
117     @Override
118     protected List<JcrItemAdapter> getItems() {
119         final List<JcrItemAdapter> allItems = Lists.newLinkedList();
120 
121         for (JcrItemAdapter resourceItem : super.getItems()) {
122             allItems.addAll(resolveJcrItemsToPublish((JcrNodeAdapter) resourceItem));
123         }
124         return allItems;
125     }
126 
127     private List<JcrItemAdapter> resolveJcrItemsToPublish(JcrNodeAdapter jcrItemAdapter) {
128         List<JcrItemAdapter> items = Lists.newLinkedList();
129         try {
130             if (!StringUtils.equals(definition.getCommand(), "unpublish") || !StringUtils.equals(definition.getCommand(), "deactivate") || !definition.isRecursive()) {
131                 int depth = jcrItemAdapter.getJcrItem().getDepth();
132                 for (int i = 1; i < depth + 1; i++) {
133                     Node ancestor = (Node) jcrItemAdapter.getJcrItem().getAncestor(i);
134                     if (!definition.isDeletionActivation() && isNodeNotActivated(ancestor)) {
135                         items.add(new JcrNodeAdapter(ancestor));
136                     } else if (definition.isDeletionActivation() && isNodeDeleted(ancestor)) {
137                         items.add(new JcrNodeAdapter(ancestor));
138                     }
139                 }
140             }
141         } catch (RepositoryException e) {
142             log.error("Resource cannot be resolved", e);
143         }
144 
145         // This block used for simple activation operation, as well as recursive activation and deactivate requests.
146         if (items.isEmpty()) {
147             items.add(new JcrNodeAdapter(jcrItemAdapter.getJcrItem()));
148         }
149 
150         return items;
151     }
152 
153     /**
154      * Returns {@code true}, If node is deleted.
155      */
156     private boolean isNodeDeleted(Node ancestor) {
157         try {
158             return NodeUtil.hasMixin(ancestor, NodeTypes.Deleted.NAME);
159         } catch (RepositoryException e) {
160             log.error("Node is not being found.", e);
161             return false;
162         }
163     }
164 
165     /**
166      * Returns {@code true}, If node is not activated.
167      */
168     private boolean isNodeNotActivated(Node node) {
169         try {
170             return !NodeTypes.Activatable.isActivated(node);
171         } catch (RepositoryException e) {
172             log.error("Node is not being found.", e);
173             return false;
174         }
175     }
176 
177     @Override
178     protected List<JcrItemAdapter> getSortedItems(Comparator<JcrItemAdapter> comparator) {
179         return Ordering.from(comparator).sortedCopy(getItems());
180     }
181 
182     // Refreshing the resources container after activation action.
183     // Since this action might activate deletion as well, thus, in that case we should be activating the parent node for reflecting changes to UI.
184     @Override
185     protected void onPostExecute() throws Exception {
186         super.onPostExecute();
187         eventBus.fireEvent(new ContentChangedEvent(changedItemPaths));
188     }
189 }