View Javadoc
1   /**
2    * This file Copyright (c) 2010-2014 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.module.admininterface.templates;
35  
36  import java.util.Collection;
37  import java.util.Iterator;
38  
39  import javax.jcr.Node;
40  import javax.jcr.RepositoryException;
41  import javax.jcr.UnsupportedRepositoryOperationException;
42  import javax.jcr.version.Version;
43  import org.slf4j.Logger;
44  import org.slf4j.LoggerFactory;
45  
46  import info.magnolia.cms.core.Content;
47  import info.magnolia.cms.core.ItemType;
48  import info.magnolia.cms.core.version.ContentVersion;
49  import info.magnolia.cms.security.AccessDeniedException;
50  import info.magnolia.cms.util.ContentUtil;
51  import info.magnolia.cms.util.ContentWrapper;
52  import info.magnolia.module.admininterface.VersionUtil;
53  import info.magnolia.rendering.model.RenderingModel;
54  import info.magnolia.rendering.model.RenderingModelImpl;
55  import info.magnolia.rendering.template.RenderableDefinition;
56  
57  public class MgnlDeletedTemplateModel extends RenderingModelImpl<RenderableDefinition> {
58  
59      private static final Logger log = LoggerFactory.getLogger(MgnlDeletedTemplateModel.class);
60  
61      public MgnlDeletedTemplateModel(Node content, RenderableDefinition definition, RenderingModel<RenderableDefinition> parent) {
62          super(content, definition, parent);
63      }
64  
65      public String getLastVersion() {
66          Collection<Version> allVersions = null;
67  
68          Iterator<Version> iterator = null;
69          Content maybeVersion = ContentUtil.asContent(getNode());
70          Content content = ContentUtil.asContent(getNode());
71          try {
72              try {
73                  allVersions = VersionUtil.getSortedNotDeletedVersions(content);
74              } catch (AccessDeniedException e) {
75                  // Either change the ContentVersion API to delegate call to getAllVersions() to the underlying real node or do this. There is no save way of finding the node is a version since it can be wrapped by multiple different wrappers
76                  while (maybeVersion != null && maybeVersion instanceof ContentWrapper) {
77                      maybeVersion = ((ContentWrapper) maybeVersion).getWrappedContent();
78                  }
79                  if (maybeVersion instanceof ContentVersion) {
80                      String versionName = ((ContentVersion) maybeVersion).getVersionLabel();
81                      content = maybeVersion.getHierarchyManager().getContent(maybeVersion.getHandle());
82                      allVersions = VersionUtil.getSortedNotDeletedVersionsBefore(content, versionName);
83                  }
84              }
85  
86              if (allVersions == null || allVersions.isEmpty()) {
87                  return null;
88              }
89              return allVersions.iterator().next().getName();
90          } catch (UnsupportedRepositoryOperationException e) {
91              // if this node doesn't support versioning it can't be undeleted ... nor can we display previous version of the content
92              log.error("Failed to retrieve version history for " + content.getHandle() + ". This node doesn't support versioning", e);
93          } catch (RepositoryException e) {
94              log.error("Failed to retrieve version history for " + content.getHandle() + "", e);
95          }
96          return null;
97      }
98  
99      public boolean hasChildren() throws RepositoryException {
100         return getNode().hasNode(ItemType.CONTENT.getSystemName());
101     }
102 
103     public String getDeletionAuthor() throws RepositoryException {
104         return getNode().getProperty("mgnl:deletedBy").getString();
105     }
106 
107     public String getDeletionDate() throws RepositoryException {
108         return getNode().getProperty("mgnl:deletedOn").getString();
109     }
110 }