001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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, please see the
018 * company website: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.ade.sitemap.shared;
029
030import org.opencms.ade.detailpage.CmsDetailPageInfo;
031import org.opencms.file.CmsResource;
032import org.opencms.gwt.shared.property.CmsClientProperty;
033import org.opencms.gwt.shared.property.CmsPropertyModification;
034import org.opencms.util.CmsUUID;
035
036import java.util.ArrayList;
037import java.util.HashMap;
038import java.util.List;
039import java.util.Map;
040
041import com.google.gwt.user.client.rpc.IsSerializable;
042
043/**
044 * Bean containing sitemap entry change information.<p>
045 *
046 * @since 8.0.0
047 */
048public class CmsSitemapChange implements IsSerializable, Comparable<CmsSitemapChange> {
049
050    /** The change types. */
051    public enum ChangeType {
052        /** Making a detail page the default. */
053        bumpDetailPage,
054        /** The clip-board only change. */
055        clipboardOnly,
056        /** The create/new change. */
057        create,
058        /** The delete resource change. */
059        delete,
060        /** The modify change. */
061        modify,
062        /** The remove from navigation change. */
063        remove,
064        /** The undelete resource change. */
065        undelete
066    }
067
068    /** The change type. */
069    private ChangeType m_changeType;
070
071    /** The changed clip-board data. */
072    private CmsSitemapClipboardData m_clipBoardData;
073
074    /** The folder type to use when a subsitemap should be created, else null. */
075    private String m_createSitemapFolderType;
076
077    /** The default file id. */
078    private CmsUUID m_defaultFileId;
079
080    /** The default file's properties. */
081    private Map<String, CmsClientProperty> m_defaultFileInternalProperties = new HashMap<String, CmsClientProperty>();
082
083    /** Detail page info's to change. */
084    private List<CmsDetailPageInfo> m_detailPageInfos;
085
086    /** The entry id. */
087    private CmsUUID m_entryId;
088
089    /** Indicates if the entry to change is a leaf type entry. */
090    private boolean m_isLeafType;
091
092    /** The entry name. */
093    private String m_name;
094
095    /** The new entry copy resource structure id. */
096    private CmsUUID m_newCopyResourceId;
097
098    /** The new entry resource type id. */
099    private int m_newResourceTypeId;
100
101    /** The changed entry's own properties. */
102    private Map<String, CmsClientProperty> m_ownInternalProperties = new HashMap<String, CmsClientProperty>();
103
104    /** An additional parameter which may contain additional information for creating a new resource. */
105    private String m_parameter;
106
107    /** The entry parent id. */
108    private CmsUUID m_parentId;
109
110    /** The entry position.*/
111    private int m_position = -1;
112
113    /** The list of property modifications. */
114    private List<CmsPropertyModification> m_propertyModifications = new ArrayList<CmsPropertyModification>();
115
116    /** The entry site path. */
117    private String m_sitePath;
118
119    /** The updated entry. */
120    private CmsClientSitemapEntry m_updatedEntry;
121
122    /**
123     * Constructor needed for serialization.<p>
124     */
125    public CmsSitemapChange() {
126
127        // nothing to do
128    }
129
130    /**
131     * Constructor.<p>
132     *
133     * @param entryId entry id
134     * @param sitePath the entry site-path
135     * @param changeType the change type
136     */
137    public CmsSitemapChange(CmsUUID entryId, String sitePath, ChangeType changeType) {
138
139        m_entryId = entryId;
140        m_sitePath = sitePath;
141        m_changeType = changeType;
142    }
143
144    /**
145     * Adds the given change data to this change object.<p>
146     *
147     * @param data the change data to add
148     */
149    public void addChangeData(CmsSitemapChange data) {
150
151        if (data == null) {
152            return;
153        }
154        if (!m_entryId.equals(data.m_entryId)) {
155            throw new UnsupportedOperationException("Can't add data for a different entry id.");
156        }
157        m_sitePath = data.m_sitePath;
158        if (data.hasChangedName()) {
159            m_name = data.m_name;
160        }
161        if (data.hasChangedPosition()) {
162            m_position = data.m_position;
163        }
164        if (data.hasChangedProperties()) {
165            m_propertyModifications.addAll(data.m_propertyModifications);
166        }
167        if (data.hasNewParent()) {
168            m_parentId = data.m_parentId;
169        }
170
171        if (data.getClipBoardData() != null) {
172            m_clipBoardData = data.getClipBoardData();
173        }
174        if ((data.m_changeType == ChangeType.delete)
175            || ((data.m_changeType == ChangeType.remove) && (m_changeType != ChangeType.delete))
176            || (m_changeType == ChangeType.clipboardOnly)) {
177            m_changeType = data.m_changeType;
178        }
179    }
180
181    /**
182     * Adds a property change for a changed title.<p>
183     *
184     * @param title the changed title
185     */
186    public void addChangeTitle(String title) {
187
188        CmsPropertyModification propChange = new CmsPropertyModification(
189            m_entryId,
190            CmsClientProperty.PROPERTY_NAVTEXT,
191            title,
192            true);
193        m_propertyModifications.add(propChange);
194        m_ownInternalProperties.put("NavText", new CmsClientProperty("NavText", title, null));
195    }
196
197    /**
198     * Will compare the parent site path and the position of the entry to change.<p>
199     *
200     * @see java.lang.Comparable#compareTo(java.lang.Object)
201     */
202    public int compareTo(CmsSitemapChange arg0) {
203
204        if (m_entryId.equals(arg0.m_entryId)) {
205            return 0;
206        }
207        int result = CmsResource.getParentFolder(m_sitePath).compareTo(CmsResource.getParentFolder(arg0.m_sitePath));
208        if (result == 0) {
209            result = m_position - arg0.m_position;
210            if (result == 0) {
211                // two different entries should never have the same parent and the same name, so this compare should be sufficient
212                result = m_name.compareTo(arg0.m_name);
213            }
214        }
215        return result;
216    }
217
218    /**
219     * Two sitemap change objects are considered equal, if the entry id's are equal.<p>
220     *
221     * @see java.lang.Object#equals(java.lang.Object)
222     */
223    @Override
224    public boolean equals(Object obj) {
225
226        if (obj instanceof CmsSitemapChange) {
227            return m_entryId.equals(((CmsSitemapChange)obj).m_entryId);
228        }
229        return false;
230    }
231
232    /**
233     * Returns the change type.<p>
234     *
235     * @return the change type
236     */
237    public ChangeType getChangeType() {
238
239        return m_changeType;
240    }
241
242    /**
243     * Returns the clip-board data.<p>
244     *
245     * @return the clip-board data
246     */
247    public CmsSitemapClipboardData getClipBoardData() {
248
249        return m_clipBoardData;
250    }
251
252    /**
253     * Returns an additional parameter for creating new resources.<p>
254     *
255     * @return an additional parameter which may contain information needed to create new resources
256     */
257    public String getCreateParameter() {
258
259        return m_parameter;
260    }
261
262    /**
263     * Gets the folder type to use if a subsitemap should be created, else null.<p>
264     *
265     * @return a resource type name
266     */
267    public String getCreateSitemapFolderType() {
268
269        return m_createSitemapFolderType;
270    }
271
272    /**
273     * Gets the default file id.<p>
274     *
275     * @return the default file id
276     */
277    public CmsUUID getDefaultFileId() {
278
279        return m_defaultFileId;
280    }
281
282    /**
283     * Returns the change'S properties for the default file.<p>
284     *
285     * @return the properties for the default file
286     */
287    public Map<String, CmsClientProperty> getDefaultFileProperties() {
288
289        return m_defaultFileInternalProperties;
290    }
291
292    /**
293     * Returns the detail page info's.<p>
294     *
295     * @return the detail page info's
296     */
297    public List<CmsDetailPageInfo> getDetailPageInfos() {
298
299        return m_detailPageInfos;
300    }
301
302    /**
303     * Returns the entry id.<p>
304     *
305     * @return the entry id
306     */
307    public CmsUUID getEntryId() {
308
309        return m_entryId;
310    }
311
312    /**
313     * Returns the entry name.<p>
314     *
315     * @return the entry name
316     */
317    public String getName() {
318
319        return m_name;
320    }
321
322    /**
323     * Returns the new entry copy resource structure id.<p>
324     *
325     * @return the new entry copy resource structure id
326     */
327    public CmsUUID getNewCopyResourceId() {
328
329        return m_newCopyResourceId;
330    }
331
332    /**
333     * Returns the new entry resource type id.<p>
334     *
335     * @return the new entry resource type id
336     */
337    public int getNewResourceTypeId() {
338
339        return m_newResourceTypeId;
340    }
341
342    /**
343     * Returns the properties for the entry itself.<p>
344     *
345     * @return the properties for the entry itself
346     */
347    public Map<String, CmsClientProperty> getOwnInternalProperties() {
348
349        return m_ownInternalProperties;
350    }
351
352    /**
353     * Returns the change's properties for the entry itself.<p>
354     *
355     * @return the change's properties for the entry itself
356     */
357    public Map<String, CmsClientProperty> getOwnProperties() {
358
359        return m_ownInternalProperties;
360    }
361
362    /**
363     * Returns the entry parent id.<p>
364     *
365     * @return the entry parent id
366     */
367    public CmsUUID getParentId() {
368
369        return m_parentId;
370    }
371
372    /**
373     * Returns the entry position.<p>
374     *
375     * @return the entry position
376     */
377    public int getPosition() {
378
379        return m_position;
380    }
381
382    /**
383     * Gets the list of property changes.<p>
384     *
385     * @return the list of property changes
386     */
387    public List<CmsPropertyModification> getPropertyChanges() {
388
389        return m_propertyModifications;
390    }
391
392    /**
393     * Returns the site-path.<p>
394     *
395     * @return the site-path
396     */
397    public String getSitePath() {
398
399        return m_sitePath;
400    }
401
402    /**
403     * Returns the updated entry.<p>
404     *
405     * @return the updated entry
406     */
407    public CmsClientSitemapEntry getUpdatedEntry() {
408
409        return m_updatedEntry;
410    }
411
412    /**
413     * Returns if the entry name has changed.<p>
414     *
415     * @return <code>true</code> if the entry name has changed
416     */
417    public boolean hasChangedName() {
418
419        return m_name != null;
420    }
421
422    /**
423     * Returns if the position has changed.<p>
424     *
425     * @return <code>true</code> if the position has changed
426     */
427    public boolean hasChangedPosition() {
428
429        return m_position >= 0;
430    }
431
432    /**
433     * Returns if there are changed properties.<p>
434     *
435     * @return <code>true</code> if there are changed properties
436     */
437    public boolean hasChangedProperties() {
438
439        return (m_propertyModifications != null) && (m_propertyModifications.size() > 0);
440    }
441
442    /**
443     * Returns if detail page info's have changed.<p>
444     *
445     * @return <code>true</code> if detail page info's have changed
446     */
447    public boolean hasDetailPageInfos() {
448
449        return m_detailPageInfos != null;
450    }
451
452    /**
453     * @see java.lang.Object#hashCode()
454     */
455    @Override
456    public int hashCode() {
457
458        return m_entryId.hashCode();
459    }
460
461    /**
462     * Returns if this change sets a new parent.<p>
463     *
464     * @return <code>true</code> if the entry gets a new parent
465     */
466    public boolean hasNewParent() {
467
468        return m_parentId != null;
469    }
470
471    /**
472     * Returns if this is a deleting change.<p>
473     *
474     * @return the is delete flag
475     */
476    public boolean isDelete() {
477
478        return ChangeType.delete == m_changeType;
479    }
480
481    /**
482     * Returns if the entry to change is a leaf type entry.<p>
483     *
484     * @return <code>true</code> if the entry to change is a leaf type entry
485     */
486    public boolean isLeafType() {
487
488        return m_isLeafType;
489    }
490
491    /**
492     * Returns if this is a creating new change.<p>
493     *
494     * @return the is new flag
495     */
496    public boolean isNew() {
497
498        return ChangeType.create == m_changeType;
499    }
500
501    /**
502     * Returns if this is a remove from navigation change.<p>
503     *
504     * @return the is new flag
505     */
506    public boolean isRemove() {
507
508        return ChangeType.remove == m_changeType;
509    }
510
511    /**
512     * Sets the clip-board data.<p>
513     *
514     * @param clipBoardData the clip-board data to set
515     */
516    public void setClipBoardData(CmsSitemapClipboardData clipBoardData) {
517
518        m_clipBoardData = clipBoardData;
519    }
520
521    /**
522     * Sets additional info needed for creating new resources.<p>
523     *
524     * @param parameter the additional resource creation information
525     */
526    public void setCreateParameter(String parameter) {
527
528        m_parameter = parameter;
529
530    }
531
532    /**
533     * Sets the folder type name to use for creating a subsitemap.<p>
534     *
535     * @param folderType a resource type name
536     */
537    public void setCreateSitemapFolderType(String folderType) {
538
539        m_createSitemapFolderType = folderType;
540    }
541
542    /**
543     * Sets the default file id. <p>
544     *
545     * @param id the default file id
546     */
547    public void setDefaultFileId(CmsUUID id) {
548
549        m_defaultFileId = id;
550    }
551
552    /**
553     * Sets the properties for the default file.<p>
554     *
555     * @param props the properties for the default file
556     */
557    public void setDefaultFileInternalProperties(Map<String, CmsClientProperty> props) {
558
559        m_defaultFileInternalProperties = props;
560    }
561
562    /**
563     * Sets the detail page info's.<p>
564     *
565     * @param detailPageInfos the detail page info's to set
566     */
567    public void setDetailPageInfos(List<CmsDetailPageInfo> detailPageInfos) {
568
569        m_detailPageInfos = detailPageInfos;
570    }
571
572    /**
573     * Sets the entry id.<p>
574     *
575     * @param entryId the entry id to set
576     */
577    public void setEntryId(CmsUUID entryId) {
578
579        m_entryId = entryId;
580    }
581
582    /**
583     * Sets if the entry to change is a leaf type entry.<p>
584     *
585     * @param isLeafEntry <code>true</code> if the entry to change is a leaf type entry
586     */
587    public void setLeafType(boolean isLeafEntry) {
588
589        m_isLeafType = isLeafEntry;
590    }
591
592    /**
593     * Sets the entry name.<p>
594     *
595     * @param name the entry name to set
596     */
597    public void setName(String name) {
598
599        m_name = name;
600    }
601
602    /**
603     * Sets the new entry copy resource structure id.<p>
604     *
605     * @param newCopyResourceId the new entry copy resource structure id to set
606     */
607    public void setNewCopyResourceId(CmsUUID newCopyResourceId) {
608
609        m_newCopyResourceId = newCopyResourceId;
610    }
611
612    /**
613     * Sets the new entry resource type id.<p>
614     *
615     * @param newResourceTypeId the new entry resource type id to set
616     */
617    public void setNewResourceTypeId(int newResourceTypeId) {
618
619        m_newResourceTypeId = newResourceTypeId;
620    }
621
622    /**
623     * Sets the changed properties of the entry itself.<p>
624     *
625     * @param props the entry's changed properties
626     */
627    public void setOwnInternalProperties(Map<String, CmsClientProperty> props) {
628
629        m_ownInternalProperties = props;
630    }
631
632    /**
633     * Sets the entry parent id.<p>
634     *
635     * @param parentId the entry parent id to set
636     */
637    public void setParentId(CmsUUID parentId) {
638
639        m_parentId = parentId;
640    }
641
642    /**
643     * Sets the entry position.<p>
644     *
645     * @param position the entry position to set
646     */
647    public void setPosition(int position) {
648
649        m_position = position;
650    }
651
652    /**
653     * Sets the list of property changes.<p>
654     *
655     * @param propertyChanges the property changes
656     */
657    public void setPropertyChanges(List<CmsPropertyModification> propertyChanges) {
658
659        m_propertyModifications = propertyChanges;
660    }
661
662    /**
663     * Sets the site path.<p>
664     *
665     * @param sitePath the site path to set
666     */
667    public void setSitePath(String sitePath) {
668
669        m_sitePath = sitePath;
670    }
671
672    /**
673     * Sets the title.<p>
674     *
675     * @param title the title
676     */
677    public void setTitle(String title) {
678
679        addChangeTitle(title);
680    }
681
682    /**
683     * Sets the updated entry.<p>
684     *
685     * @param updatedEntry the updated entry to set
686     */
687    public void setUpdatedEntry(CmsClientSitemapEntry updatedEntry) {
688
689        m_updatedEntry = updatedEntry;
690    }
691}