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