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