View Javadoc
1   /**
2    * This file Copyright (c) 2012-2016 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.dam.app.setup.migration;
35  
36  import info.magnolia.module.delta.AbstractRepositoryTask;
37  
38  import java.util.Arrays;
39  
40  import javax.jcr.RepositoryException;
41  import javax.jcr.Session;
42  import javax.jcr.query.Query;
43  import javax.jcr.query.QueryManager;
44  import javax.jcr.query.QueryResult;
45  
46  import org.apache.jackrabbit.util.ISO9075;
47  import org.slf4j.Logger;
48  import org.slf4j.LoggerFactory;
49  
50  /**
51   * Abstract Migration task exposing utility methods in order to perform a
52   * search for properties containing a certain value based on some path.
53   */
54  public abstract class AbstractPropertyValueSearchDamMigrationTask extends AbstractRepositoryTask {
55  
56      private static final Logger log = LoggerFactory.getLogger(AbstractPropertyValueSearchDamMigrationTask.class);
57  
58      private String propertyValue;
59  
60      public AbstractPropertyValueSearchDamMigrationTask(String name, String description, String propertyValue) {
61          super(name, description);
62          this.propertyValue = propertyValue;
63      }
64  
65      /**
66       * Create query that search for: node of type Component and Page that has any property
67       * containing 'propertyValue' placed under 'path'.
68       */
69      protected String createQuery(String path) {
70          String encodedPath = ISO9075.encodePath(path);
71          if ("/".equals(encodedPath)) {
72              encodedPath = "";
73          }
74          return "/jcr:root" + encodedPath + "//element(*,nt:base)[jcr:contains(., '" + getPropertyValue() + "')] ";
75      }
76  
77      /**
78       * Execute the search query.
79       */
80      @SuppressWarnings("deprecation")
81      protected QueryResult executeQuery(String statement, Session session) {
82          try {
83              QueryManager jcrQueryManager = session.getWorkspace().getQueryManager();
84              // Use Xpath while Contains is badly supported by SQL2
85              Query query = jcrQueryManager.createQuery(statement, Query.XPATH);
86              QueryResult result = query.execute();
87              return result;
88          } catch (RepositoryException e) {
89              log.error("Not able to execute the following JCR-JQOM query '{}' against the following session '{}'. Exception '{}'", Arrays.asList(statement, session.getWorkspace().getName(), e.getMessage()), e);
90              return null;
91          }
92      }
93  
94      protected void setPropertyValue(String propertyValue) {
95          this.propertyValue = propertyValue;
96      }
97  
98      protected String getPropertyValue() {
99          return this.propertyValue;
100     }
101 
102     /**
103      * Based on propertyName build a i18n suffixed property name.<br>
104      * - if the propertyName has no local suffix (myPropertyName) <br>
105      * return propertyName+propertyNameSufix (propertyNamepropertyNameSufix)<br>
106      * - if the propertyName has a valid i18n suffix (myPropertyName_en)<br>
107      * return propertyName+propertyNameSufix + i18n suffix (propertyNamepropertyNameSufix_en).
108      *
109      * @param propertyName root property name.
110      * @param propertyNameSuffix suffix to add to the property name
111      * @return
112      * @deprecated since 2.0.3 use {@link DamMigrationUtil#buildI18nSuffixPropertyName(String, String)}.
113      */
114     @Deprecated
115     protected String buildI18nSuffixPropertyName(String propertyName, String propertyNameSuffix) {
116         return DamMigrationUtil.buildI18nSuffixPropertyName(propertyName, propertyNameSuffix);
117     }
118 
119     /**
120      * @deprecated since 2.0.3 use {@link DamMigrationUtil#isPropertyNameI18nBased(String)}.
121      */
122     @Deprecated
123     protected boolean isPropertyNameI18nBased(String propertyName) {
124         return DamMigrationUtil.isPropertyNameI18nBased(propertyName);
125     }
126 
127     /**
128      * @param propertyName (propertyName_en return en, propertyName return null)
129      * @return the local of the property id it's a i18n propertyName. Null otherwise
130      * @deprecated since 2.0.3 use {@link DamMigrationUtil#getI18nLocalBasedOnPropertyName(String)}.
131      */
132     @Deprecated
133     protected String getI18nLocalBasedOnPropertyName(String propertyName) {
134         return DamMigrationUtil.getI18nLocalBasedOnPropertyName(propertyName);
135     }
136 
137     /**
138      * @deprecated since 2.0.3 use {@link DamMigrationUtil#getPropertyNameWithoutLocalSuffix(String)}.
139      */
140     @Deprecated
141     protected String getPropertyNameWithoutLocalSuffix(String propertyName) {
142         return DamMigrationUtil.getPropertyNameWithoutLocalSuffix(propertyName);
143     }
144 }