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.LinkedList;
020import java.util.List;
021
022import org.ametys.runtime.i18n.I18nizableText;
023
024/**
025 * The report object, used internally to provide useful informations once
026 * the whole duplication process has ended.
027 */
028public final class CopyReport
029{
030    private String _viewName;
031    private String _fallbackViewName;
032    private String _baseContentId;
033    private String _baseContentTitle;
034    private Boolean _baseContentIsReferenceTable;
035    
036    private CopyMode _mode;
037    
038    private String _targetContentId;
039    private String _targetContentTitle;
040    private boolean _targetContentIsReferenceTable;
041    
042    private List<CopyReport> _innerReports;
043    
044    private List<String> _copiedAttachments;
045    
046    private CopyState _state;
047    private I18nizableText _errorMsg;
048    
049    /** Available copy mode */
050    public enum CopyMode
051    {
052        /** Creation : a new content is created to perform the copy */
053        CREATION,
054        /** Edition : attributes are duplicated into an already existing target content */
055        EDITION
056    }
057    
058    /** The possible state of the copy */
059    public enum CopyState
060    {
061        /** Running : when the copy is still running */
062        RUNNING,
063        /** Success : when the copy has ended well */
064        SUCCESS,
065        /** Error : when an error was encountered during the copy (except for attributes copy errors). */
066        ERROR
067    }
068    
069    /**
070     * Constructor.
071     * If the title of the base content if available, should be used.
072     * @param baseContentId The content id of the base content
073     * @param baseContentIsRefTable Indicates if the base content is a reference table's entry (can be null if unavailable)
074     * @param viewName The view name
075     * @param fallbackViewName The fallback view name. Use for legacy purpose.
076     * @param copyMode The mode of copy
077     */
078    public CopyReport(String baseContentId, Boolean baseContentIsRefTable, String viewName, String fallbackViewName, CopyMode copyMode)
079    {
080        this(baseContentId, null, baseContentIsRefTable, viewName, fallbackViewName, copyMode);
081    }
082    
083    /**
084     * Constructor.
085     * @param baseContentId The content id of the base content
086     * @param baseContentTitle The title of the base content
087     * @param baseContentIsRefTable Indicates if the base content is a reference table's entry
088     * @param viewName The view name
089     * @param fallbackViewName The fallback view name. Use for legacy purpose.
090     * @param copyMode The mode of copy
091     */
092    public CopyReport(String baseContentId, String baseContentTitle, Boolean baseContentIsRefTable, String viewName, String fallbackViewName, CopyMode copyMode)
093    {
094        _baseContentId = baseContentId;
095        _baseContentTitle = baseContentTitle;
096        _baseContentIsReferenceTable = baseContentIsRefTable;
097        _viewName = viewName;
098        _fallbackViewName = fallbackViewName;
099        _mode = copyMode;
100        
101        _innerReports = new LinkedList<>();
102        _copiedAttachments = new LinkedList<>();
103        
104        _state = CopyState.RUNNING;
105        
106        _errorMsg = null;
107    }
108    
109    /**
110     * Set whether the base content is a reference table's entry.
111     * @param isRefTable true for reference table's entry.
112     */
113    public void setReferenceTable(boolean isRefTable)
114    {
115        _baseContentIsReferenceTable = isRefTable;
116    }
117    
118    /**
119     * Set the target content title.
120     * @param title The target content title.
121     */
122    public void setTargetContentTitle(String title)
123    {
124        _targetContentTitle = title;
125    }
126    
127    /**
128     * Notify information about the target content to the report.
129     * @param id The content id
130     * @param title The content title
131     * @param isRefTable Indicates if the target content is a reference table's entry.
132     */
133    public void notifyContentCreation(String id, String title, boolean isRefTable)
134    {
135        _targetContentId = id;
136        _targetContentTitle = title;
137        _targetContentIsReferenceTable = isRefTable;
138    }
139    
140    /**
141     * Notify a copy success
142     */
143    public void notifyContentCopySuccess()
144    {
145        _state = CopyState.SUCCESS;
146    }
147    
148    /**
149     * Notify that the copy ended with an error
150     */
151    public void notifyContentCopyError()
152    {
153        _state = CopyState.ERROR;
154    }
155    
156    /**
157     * Set the error message
158     * @param errorMsg the error message
159     */
160    public void setErrorMessage(I18nizableText errorMsg)
161    {
162        _errorMsg = errorMsg;
163    }
164    
165    /**
166     * Get the error message
167     * @return the error message. Can be null.
168     */
169    public I18nizableText getErrorMessage()
170    {
171        return _errorMsg;
172    }
173    
174    /**
175     * Add an attachments to the copied attachments report list
176     * @param relPath The relative path where to copy
177     */
178    public void addAttachment(String relPath)
179    {
180        _copiedAttachments.add(relPath);
181    }
182    
183    /**
184     * Get the status of the copy
185     * @return A copy state enum value
186     */
187    public CopyState getStatus()
188    {
189        return _state;
190    }
191    
192    /**
193     * Get the mode of the copy
194     * @return A cope mode enum value
195     */
196    public CopyMode getMode()
197    {
198        return _mode;
199    }
200    
201    /**
202     * Get the name of the view used for the copy.
203     * @return the view name
204     */
205    public String getViewName()
206    {
207        return _viewName;
208    }
209    
210    /**
211     * Get the name of the fallback view used for the copy.
212     * @return the fallback view name
213     */
214    public String getFallbackViewName()
215    {
216        return _fallbackViewName;
217    }
218    
219    /**
220    /**
221     * Get the base content identifier
222     * @return The identifier
223     */
224    public String getBaseContentId()
225    {
226        return _baseContentId;
227    }
228    
229    /**
230     * Get the base content title.
231     * @return The title or null (if unavailable)
232     */
233    public String getBaseContentTitle()
234    {
235        return _baseContentTitle;
236    }
237    
238    /**
239     * Is the base content a reference table entry ?
240     * @return true if it is, null if unknown.
241     */
242    public Boolean getBaseContentIsReferenceTable()
243    {
244        return _baseContentIsReferenceTable;
245    }
246    
247    /**
248     * Get the target content identifier
249     * @return The identifier
250     */
251    public String getTargetContentId()
252    {
253        return _targetContentId;
254    }
255    
256    /**
257     * Get the target content title
258     * @return The title
259     */
260    public String getTargetContentTitle()
261    {
262        return _targetContentTitle;
263    }
264    
265    /**
266     * Is the target content a reference table entry?
267     * @return true if it is.
268     */
269    public boolean getTargetContentIsReferenceTable()
270    {
271        return _targetContentIsReferenceTable;
272    }
273    
274    /**
275     * Get the list of child copy reports.
276     * Each child report denotes an inner copy
277     * @return The list of copy reports.
278     */
279    public List<CopyReport> getChildReports()
280    {
281        return Collections.unmodifiableList(_innerReports);
282    }
283    
284    /**
285     * Get the list of the copied attachments.
286     * @return List of paths (relative to the root attachment node of the target content)
287     */
288    public List<String> getCopiedAttachments()
289    {
290        return Collections.unmodifiableList(_copiedAttachments);
291    }
292    
293    /**
294     * Add a child report to the report. This must be done when an copy is requested 
295     * @param report The report of the copy to add
296     */
297    public void addReport(CopyReport report)
298    {
299        _innerReports.add(report);
300    }
301}