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