001/*
002 *  Copyright 2013 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.cms.content;
017
018import java.util.Collections;
019import java.util.HashMap;
020import java.util.LinkedList;
021import java.util.List;
022import java.util.Map;
023
024import org.ametys.cms.contenttype.MetadataDefinition;
025import org.ametys.plugins.repository.metadata.ModifiableRichText;
026import org.ametys.runtime.i18n.I18nizableText;
027
028/**
029 * The report object, used internally to provide useful informations once
030 * the whole duplication process has ended.
031 */
032public final class CopyReport
033{
034    /** Name of the metadata set associated to this report */
035    protected final String _metadataSetName;
036    /** Name of the metadata set associated to this report */
037    protected final String _fallbackMetadataSetName;
038    /** Type of the metadata set associated to this report */
039    protected final String _metadataSetType;
040    /** Identifier of the base content associated to this report */
041    protected final String _baseContentId;
042    /** Title of the base content associated to this report (can be null if unavailable) */
043    protected final String _baseContentTitle;
044    /** Indicates if the base content associated to this report is a a entry of a reference table (can be null if unavailable) */
045    protected Boolean _baseContentIsReferenceTable;
046    
047    /** The copy mode used during the copy (creation or edition) */
048    protected final CopyMode _mode;
049    
050    /** Identifier of the target content associated to this report */
051    protected String _targetContentId;
052    /** title of the target content associated to this report */
053    protected String _targetContentTitle;
054    /** Indicates if the target content associated to this report is a entry of a reference table */
055    protected boolean _targetContentIsReferenceTable;
056    
057    /** Child reports associated to this report. Each child report denotes an inner copy */
058    protected List<CopyReport> _innerReports;
059    
060    /** List of copied rich text metadata during the duplication process */ 
061    protected Map<ModifiableRichText, MetadataDefinition> _copiedRichTexts;
062    /**
063     * List of paths (relative to the root attachment node of the target
064     * content) of the copied attachments during the duplication
065     */ 
066    protected List<String> _copiedAttachments;
067    
068    /**
069     * List of values (content id or list of content ids) to be set into
070     * metadata of type content. Keys are the metadata path
071     */
072    protected Map<String, Object> _contentReferenceValues;
073    
074    /** List of info about repeaters to be used to perform the edition of content metadata */
075    protected Map<String, Integer> _repeatersInfo;
076    
077    /** List of metadata labels. Each entry in the list denote an error during the copy of this metadata */
078    protected List<I18nizableText> _metadataCopyErrors;
079    
080    /** The state of the copy */
081    protected CopyState _state;
082    
083    /** Available copy mode */
084    public enum CopyMode
085    {
086        /** Creation : a new content is created to perform the copy */
087        CREATION,
088        /** Edition : metadata are duplication into an already existing target content */
089        EDITION
090    }
091    
092    /** The possible state of the copy */
093    public enum CopyState
094    {
095        /** Running : when the copy is still running */
096        RUNNING,
097        /** Success : when the copy has ended well */
098        SUCCESS,
099        /** Error : when an error was encountered during the copy (except for metadata copy errors). */
100        ERROR
101    }
102    
103    /**
104     * Constructor.
105     * If the title of the base content if available, should be used.
106     * @param baseContentId The content id of the base content
107     * @param baseContentIsRefTable Indicates if the base content is a reference table's entry (can be null if unavailable)
108     * @param metadataSetName The metadata set name
109     * @param fallbackMetadataSetName The fallback metadata set name. Use for legacy purpose.
110     * @param metadataSetType The metadata set type (edit or view)
111     * @param copyMode The mode of copy
112     */
113    public CopyReport(String baseContentId, Boolean baseContentIsRefTable, String metadataSetName, String fallbackMetadataSetName, String metadataSetType, CopyMode copyMode)
114    {
115        this(baseContentId, null, baseContentIsRefTable, metadataSetName, fallbackMetadataSetName, metadataSetType, copyMode);
116    }
117    
118    /**
119     * Constructor.
120     * @param baseContentId The content id of the base content
121     * @param baseContentTitle The title of the base content
122     * @param baseContentIsRefTable Indicates if the base content is a reference table's entry
123     * @param metadataSetName The metadata set name
124     * @param fallbackMetadataSetName The fallback metadata set name. Use for legacy purpose.
125     * @param metadataSetType The metadata set type (edit or view)
126     * @param copyMode The mode of copy
127     */
128    public CopyReport(String baseContentId, String baseContentTitle, Boolean baseContentIsRefTable, String metadataSetName, String fallbackMetadataSetName, String metadataSetType, CopyMode copyMode)
129    {
130        _baseContentId = baseContentId;
131        _baseContentTitle = baseContentTitle;
132        _baseContentIsReferenceTable = baseContentIsRefTable;
133        _metadataSetName = metadataSetName;
134        _fallbackMetadataSetName = fallbackMetadataSetName;
135        _metadataSetType = metadataSetType;
136        _mode = copyMode;
137        
138        _innerReports = new LinkedList<>();
139        _copiedRichTexts = new HashMap<>();
140        _copiedAttachments = new LinkedList<>();
141        _contentReferenceValues = new HashMap<>();
142        _repeatersInfo = new HashMap<>();
143        _metadataCopyErrors = new LinkedList<>();
144        
145        _state = CopyState.RUNNING;
146    }
147    
148    /**
149     * Set whether the base content is a reference table's entry.
150     * @param isRefTable true for reference table's entry.
151     */
152    public void setReferenceTable(boolean isRefTable)
153    {
154        _baseContentIsReferenceTable = isRefTable;
155    }
156    
157    /**
158     * Set the target content title.
159     * @param title The target content title.
160     */
161    public void setTargetContentTitle(String title)
162    {
163        _targetContentTitle = title;
164    }
165    
166    /**
167     * Notify information about the target content to the report.
168     * @param id The content id
169     * @param title The content title
170     * @param isRefTable Indicates if the target content is a reference table's entry.
171     */
172    public void notifyContentCreation(String id, String title, boolean isRefTable)
173    {
174        _targetContentId = id;
175        _targetContentTitle = title;
176        _targetContentIsReferenceTable = isRefTable;
177    }
178    
179    /**
180     * Notify a copy success
181     */
182    public void notifyContentCopySuccess()
183    {
184        _state = CopyState.SUCCESS;
185    }
186    
187    /**
188     * Notify that the copy ended with an error (except for metadata copy
189     * error, where {@link #notifyMetadataCopyError(I18nizableText)} must be
190     * used).
191     */
192    public void notifyContentCopyError()
193    {
194        _state = CopyState.ERROR;
195    }
196    
197    /**
198     * Notify a metadata copy error.
199     * @param label The label of the metadata
200     */
201    public void notifyMetadataCopyError(I18nizableText label)
202    {
203        _metadataCopyErrors.add(label);
204    }
205    
206    /**
207     * Add a rich text to the list of copied rich texts.
208     * @param richText the rich text
209     * @param definition the metadata definition
210     */
211    public void addRichText(ModifiableRichText richText, MetadataDefinition definition)
212    {
213        _copiedRichTexts.put(richText, definition);
214    }
215    
216    /**
217     * Get the list of copied rich text metadata.
218     * @return List of {@link ModifiableRichText}
219     */
220    public Map<ModifiableRichText, MetadataDefinition> getCopiedRichTexts()
221    {
222        return Collections.unmodifiableMap(_copiedRichTexts);
223    }
224    
225    /**
226     * Add an attachments to the copied attachments report list
227     * @param relPath The relative path where to copy
228     */
229    public void addAttachment(String relPath)
230    {
231        _copiedAttachments.add(relPath);
232    }
233    
234    /**
235     * Should be used to store the values of a content metadata.
236     * These values will be used later in the copy process to properly edit content metadata
237     * @param metadataPath The path of the metadata to copy
238     * @param values The values
239     */
240    public void addContentReferenceValues(String metadataPath, Object values)
241    {
242        _contentReferenceValues.put(metadataPath, values);
243    }
244    
245    /**
246     * Get the list content reference values 
247     * @return Map representing the content reference values
248     */
249    public Map<String, Object> getContentReferenceValuesMap()
250    {
251        return Collections.unmodifiableMap(_contentReferenceValues);
252    }
253    
254    /**
255     * Add repeater info to be able to perform an edition at the of the copy for content metadata
256     * @param metadataPath The path of the metadata concerned
257     * @param entryCount the entry count
258     */
259    public void addRepeaterInfo(String metadataPath, int entryCount)
260    {
261        _repeatersInfo.put(metadataPath, entryCount);
262    }
263    
264    /**
265     * Get the map of info about copied repeaters
266     * @return Map holding repeaters info. Each values is the size of a repeater
267     */
268    public Map<String, Integer> getRepeatersInfo()
269    {
270        return Collections.unmodifiableMap(_repeatersInfo);
271    }
272    
273    /*
274     * PUBLIC API
275     */
276    /**
277     * Get the status of the copy
278     * @return A copy state enum value
279     */
280    public CopyState getStatus()
281    {
282        return _state;
283    }
284    
285    /**
286     * Get the mode of the copy
287     * @return A cope mode enum value
288     */
289    public CopyMode getMode()
290    {
291        return _mode;
292    }
293    
294    /**
295     * Get the name of the metadataset used for the copy.
296     * @return the metadataset name
297     */
298    public String getMetadataSetName()
299    {
300        return _metadataSetName;
301    }
302    
303    /**
304     * Get the name of the fallback metadataset used for the copy.
305     * @return the fallback metadataset name
306     */
307    public String getFallbackMetadataSetName()
308    {
309        return _fallbackMetadataSetName;
310    }
311    
312    /**
313     * Get the type of the metadataset used for the copy.
314     * @return the metadataset name
315     */
316    public String getMetadataSetType()
317    {
318        return _metadataSetType;
319    }
320    
321    /**
322     * Get the base content identifier
323     * @return The identifier
324     */
325    public String getBaseContentId()
326    {
327        return _baseContentId;
328    }
329    
330    /**
331     * Get the base content title.
332     * @return The title or null (if unavailable)
333     */
334    public String getBaseContentTitle()
335    {
336        return _baseContentTitle;
337    }
338    
339    /**
340     * Is the base content a reference table entry ?
341     * @return true if it is, null if unknown.
342     */
343    public Boolean getBaseContentIsReferenceTable()
344    {
345        return _baseContentIsReferenceTable;
346    }
347    
348    /**
349     * Get the target content identifier
350     * @return The identifier
351     */
352    public String getTargetContentId()
353    {
354        return _targetContentId;
355    }
356    
357    /**
358     * Get the target content title
359     * @return The title
360     */
361    public String getTargetContentTitle()
362    {
363        return _targetContentTitle;
364    }
365    
366    /**
367     * Is the target content a reference table entry?
368     * @return true if it is.
369     */
370    public boolean getTargetContentIsReferenceTable()
371    {
372        return _targetContentIsReferenceTable;
373    }
374    
375    /**
376     * Get the list of child copy reports.
377     * Each child report denotes an inner copy
378     * @return The list of copy reports.
379     */
380    public List<CopyReport> getChildReports()
381    {
382        return Collections.unmodifiableList(_innerReports);
383    }
384    
385    /**
386     * Get the list of metadata errors during the copy.
387     * @return List of the label of each metadata in error.
388     */
389    public List<I18nizableText> getMetadataErrors()
390    {
391        return Collections.unmodifiableList(_metadataCopyErrors);
392    }
393    
394    
395    /**
396     * Get the list of the copied attachments.
397     * @return List of paths (relative to the root attachment node of the target content)
398     */
399    public List<String> getCopiedAttachments()
400    {
401        return Collections.unmodifiableList(_copiedAttachments);
402    }
403    
404    /**
405     * Add a child report to the report. This must be done when an copy is requested 
406     * @param report The report of the copy to add
407     */
408    public void addReport(CopyReport report)
409    {
410        _innerReports.add(report);
411    }
412}