001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ui.apps.search;
029
030import org.opencms.file.CmsPropertyDefinition;
031import org.opencms.file.CmsResource;
032import org.opencms.ui.apps.search.CmsSourceSearchForm.SearchType;
033import org.opencms.util.CmsStringUtil;
034
035import java.io.Serializable;
036import java.util.LinkedList;
037import java.util.List;
038
039/**
040 * Settings bean for the dialog.
041 * <p>
042 */
043public class CmsSearchReplaceSettings implements Serializable {
044
045    /** The serial version id. */
046    private static final long serialVersionUID = 1122133222446934991L;
047
048    /** The force replacement flag. */
049    private boolean m_forceReplace;
050
051    /** When replacing XML content, replace-operation only applies to this locale. */
052    private String m_locale;
053
054    /** Display message. */
055    private String m_message;
056
057    /** Flag indicating if only content values should be searched and replaced. */
058    private boolean m_onlyContentValues;
059
060    /** The paths to collect resources. */
061    private List<String> m_paths = new LinkedList<String>();
062
063    /** The project to use. */
064    private String m_project;
065
066    /** The search query to filter matching resources. */
067    private String m_query;
068
069    /** The replace pattern. */
070    private String m_replacepattern;
071
072    /** The list of resource paths to process: all should be files. */
073    private String[] m_resources;
074
075    /** The search pattern. */
076    private String m_searchpattern;
077
078    /** The source to retrive the resources from. */
079    private String m_source;
080
081    /** The search type. */
082    private SearchType m_type;
083
084    /** The resource type to use for replacement. */
085    private String[] m_types;
086
087    /** The Xpath to perform the replacement. */
088    private String m_xpath;
089
090    /** The site root. */
091    private String m_siteRoot;
092
093    /**Property definition for property search.*/
094    private CmsPropertyDefinition m_property = CmsPropertyDefinition.getNullPropertyDefinition();
095
096    /**Ignore subsites? */
097    private boolean m_ignoreSubSites;
098
099    /**Element to search for. */
100    private CmsResource m_element;
101
102    /**
103     * Bean constructor with cms object for path validation.<p>
104     */
105    public CmsSearchReplaceSettings() {
106
107        super();
108        m_paths.add("/");
109    }
110
111    /**
112     * Replace element pattern.<p>
113     *
114     * @param resource Resource to replace
115     * @return String regex
116     */
117    public static String replaceElementInPagePattern(CmsResource resource) {
118
119        return "<target><![CDATA["
120            + resource.getRootPath()
121            + "]]></target>//t<uuid>"
122            + resource.getStructureId().getStringValue()
123            + "</uuid>";
124    }
125
126    /**
127     * Search element in xml page pattern.<p>
128     *
129     * @param resource Resource
130     * @return String regex
131     */
132    public static String searchElementInPagePattern(CmsResource resource) {
133
134        return "<target>.{0,9}"
135            + resource.getRootPath()
136            + ".{0,3}</target>\\s*<uuid>"
137            + resource.getStructureId().getStringValue()
138            + "</uuid>";
139    }
140
141    /**
142     * Get element to search for.<p>
143     *
144     * @return CmsResource
145     */
146
147    public CmsResource getElementResource() {
148
149        return m_element;
150    }
151
152    /**
153     * Returns the locale.<p>
154     *
155     * @return the locale
156     */
157    public String getLocale() {
158
159        return m_locale;
160    }
161
162    /**
163     * @return the message
164     */
165    public String getMessage() {
166
167        return m_message;
168    }
169
170    /**
171     * @return the paths
172     */
173    public List<String> getPaths() {
174
175        return m_paths;
176    }
177
178    /**
179     * @return the project
180     */
181    public String getProject() {
182
183        return m_project;
184    }
185
186    /**
187     * Get property.<p>
188     *
189     * @return CmsPropertyDefinition
190     */
191    public CmsPropertyDefinition getProperty() {
192
193        return m_property;
194    }
195
196    /**
197     * Returns the query.<p>
198     *
199     * @return the query
200     */
201    public String getQuery() {
202
203        return m_query;
204    }
205
206    /**
207     * @return the replace pattern
208     */
209    public String getReplacepattern() {
210
211        return m_replacepattern;
212    }
213
214    /**
215     * @return the resources
216     */
217    public String getResources() {
218
219        return CmsStringUtil.arrayAsString(m_resources, ",");
220    }
221
222    /**
223     * Returns the resources paths in an array.<p>
224     *
225     * @return the resources paths in an array.
226     */
227    public String[] getResourcesArray() {
228
229        return m_resources;
230    }
231
232    /**
233     * @return the search pattern
234     */
235    public String getSearchpattern() {
236
237        return m_searchpattern;
238    }
239
240    /**
241     * Returns the site root.<p>
242     *
243     * @return the siteRoot
244     */
245    public String getSiteRoot() {
246
247        return m_siteRoot;
248    }
249
250    /**
251     * Returns the source.<p>
252     *
253     * @return the source
254     */
255    public String getSource() {
256
257        return m_source;
258    }
259
260    /**
261     * Returns the search type.<p>
262     *
263     * @return the search type
264     */
265    public SearchType getType() {
266
267        return m_type;
268    }
269
270    /**
271     * Returns the resource type(s).<p>
272     *
273     * @return the type
274     */
275    public String getTypes() {
276
277        return m_types != null ? CmsStringUtil.arrayAsString(m_types, ",") : "";
278    }
279
280    /**
281     * Returns the type.<p>
282     *
283     * @return the type
284     */
285    public String[] getTypesArray() {
286
287        return m_types;
288    }
289
290    /**
291     * Returns the xpath.<p>
292     *
293     * @return the xpath
294     */
295    public String getXpath() {
296
297        return m_xpath;
298    }
299
300    /**
301     * Should subsites be ignored?
302     *
303     * @return boolean
304     */
305    public boolean ignoreSubSites() {
306
307        return m_ignoreSubSites;
308    }
309
310    /**
311     * Returns the force replace flag, if <code>true</code> the replacement
312     * will also be performed if the replacement String is empty.<p>
313     *
314     * @return the force replace flag
315     */
316    public boolean isForceReplace() {
317
318        return m_forceReplace;
319    }
320
321    /**
322     * Returns if only content values should be searched and replaced.<p>
323     *
324     * @return if only content values should be searched and replaced
325     */
326    public boolean isOnlyContentValues() {
327
328        return m_onlyContentValues;
329    }
330
331    /**
332     * Returns <code>true</code> if Solr index is selected and a query was entered.<p>
333     *
334     * @return <code>true</code> if Solr index is selected and a query was entered
335     */
336    public boolean isSolrSearch() {
337
338        return (CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_source)
339            && CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_query));
340    }
341
342    /**
343     * Set Element to search for.<p>
344     *
345     * @param resource CmsResource
346     */
347    public void setElementResource(CmsResource resource) {
348
349        m_element = resource;
350    }
351
352    /**
353     * Sets the force replace flag.<p>
354     *
355     * @param forceReplace the force replace flag to set
356     */
357    public void setForceReplace(boolean forceReplace) {
358
359        m_forceReplace = forceReplace;
360    }
361
362    /**
363     * Set if subsites should be ignored.<p>
364     *
365     * @param ignore boolean
366     */
367    public void setIgnoreSubSites(boolean ignore) {
368
369        m_ignoreSubSites = ignore;
370    }
371
372    /**
373     * Sets the locale.<p>
374     *
375     * @param locale the locale to set
376     */
377    public void setLocale(String locale) {
378
379        m_locale = locale;
380    }
381
382    /**
383     * @param message the message to set
384     */
385    public void setMessage(final String message) {
386
387        // nop, this is hardcoded... just has to be here for "bean - convention".
388    }
389
390    /**
391     * Sets if only content values should be searched and replaced.<p>
392     *
393     * @param onlyContentValue if only content values should be searched and replaced
394     */
395    public void setOnlyContentValues(boolean onlyContentValue) {
396
397        m_onlyContentValues = onlyContentValue;
398    }
399
400    /**
401     * Sets the paths.<p>
402     *
403     * @param paths the paths to set
404     */
405    public void setPaths(final List<String> paths) {
406
407        m_paths = paths;
408    }
409
410    /**
411     * @param project the project to work in
412     */
413    public void setProject(String project) {
414
415        m_project = project;
416    }
417
418    /**
419     * Sets the property.<p>
420     *
421     * @param value CmsPropertyDefinition
422     */
423    public void setProperty(CmsPropertyDefinition value) {
424
425        m_property = value;
426
427    }
428
429    /**
430     * Sets the query.<p>
431     *
432     * @param query the query to set
433     */
434    public void setQuery(String query) {
435
436        m_query = query;
437    }
438
439    /**
440     * Sets the replace pattern.<p>
441     *
442     * @param replacepattern the replace pattern
443     */
444    public void setReplacepattern(String replacepattern) {
445
446        m_replacepattern = replacepattern;
447    }
448
449    /**
450     * @param resources
451     *            the resources to set
452     */
453    public void setResources(final String resources) {
454
455        m_resources = CmsStringUtil.splitAsArray(resources, ",");
456
457    }
458
459    /**
460     * Sets the search pattern.<p>
461     *
462     * @param searchpattern the search pattern
463     */
464    public void setSearchpattern(String searchpattern) {
465
466        m_searchpattern = searchpattern;
467    }
468
469    /**
470     * Sets the site root.<p>
471     *
472     * @param siteRoot the site root to set
473     */
474    public void setSiteRoot(String siteRoot) {
475
476        m_siteRoot = siteRoot;
477    }
478
479    /**
480     * Sets the source.<p>
481     *
482     * @param source the source to set
483     */
484    public void setSource(String source) {
485
486        m_source = source;
487    }
488
489    /**
490     * Sets the search type.<p>
491     *
492     * @param type the search type to set
493     */
494    public void setType(SearchType type) {
495
496        m_type = type;
497    }
498
499    /**
500     * Sets the resource type(s).<p>
501     *
502     * @param types the type to set
503     */
504    public void setTypes(String types) {
505
506        m_types = CmsStringUtil.splitAsArray(types, ",");
507    }
508
509    /**
510     * Sets the xpath.<p>
511     *
512     * @param xpath the xpath to set
513     */
514    public void setXpath(String xpath) {
515
516        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(xpath)) {
517            xpath = xpath.trim();
518            if (xpath.startsWith("/")) {
519                xpath = xpath.substring(1);
520            }
521            if (xpath.endsWith("/")) {
522                xpath = xpath.substring(0, xpath.length() - 1);
523            }
524        }
525        m_xpath = xpath;
526    }
527}