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