001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH (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 GmbH, 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.file;
029
030import org.opencms.main.CmsIllegalArgumentException;
031import org.opencms.security.CmsOrganizationalUnit;
032import org.opencms.util.A_CmsModeIntEnumeration;
033import org.opencms.util.CmsStringUtil;
034import org.opencms.util.CmsUUID;
035
036import java.util.List;
037
038/**
039 * Describes an OpenCms project, 
040 * which contains a set of VFS resources that are being worked on at the same time.<p>
041 * 
042 * @since 6.0.0 
043 */
044public class CmsProject implements Cloneable, Comparable<CmsProject> {
045
046    /**
047     *  Enumeration class for project types.<p>
048     */
049    public static final class CmsProjectType extends A_CmsModeIntEnumeration {
050
051        /** Project type normal. */
052        protected static final CmsProjectType MODE_PROJECT_NORMAL = new CmsProjectType(0);
053
054        /** Project type temporary. */
055        protected static final CmsProjectType MODE_PROJECT_TEMPORARY = new CmsProjectType(1);
056
057        /** Project type 'workflow'. */
058        protected static final CmsProjectType MODE_PROJECT_WORKFLOW = new CmsProjectType(2);
059
060        /** Serializable version id. */
061        private static final long serialVersionUID = -8701314451776599534L;
062
063        /**
064         * Private constructor.<p>
065         * 
066         * @param mode the copy mode integer representation
067         */
068        private CmsProjectType(int mode) {
069
070            super(mode);
071        }
072
073        /**
074         * Returns the copy mode object from the old copy mode integer.<p>
075         * 
076         * @param mode the old copy mode integer
077         * 
078         * @return the copy mode object
079         */
080        public static CmsProjectType valueOf(int mode) {
081
082            switch (mode) {
083                case 0:
084                    return CmsProjectType.MODE_PROJECT_NORMAL;
085                case 1:
086                    return CmsProjectType.MODE_PROJECT_TEMPORARY;
087                case 2:
088                    return CmsProjectType.MODE_PROJECT_WORKFLOW;
089                default:
090                    return CmsProjectType.MODE_PROJECT_NORMAL;
091            }
092        }
093
094        /**
095         * Returns the default flags which should be set when a new project of this type is created.<p>
096         * 
097         * @return the default flags for the project type 
098         */
099        public int getDefaultFlags() {
100
101            //            if (getMode() == CmsProjectType.MODE_PROJECT_WORKFLOW.getMode()) {
102            //                return PROJECT_FLAG_HIDDEN;
103            //            }
104            return PROJECT_FLAG_NONE;
105        }
106    }
107
108    /** The name of the online project. */
109    public static final String ONLINE_PROJECT_NAME = "Online";
110
111    /** The id of the online project. */
112    public static final CmsUUID ONLINE_PROJECT_ID = CmsUUID.getConstantUUID(ONLINE_PROJECT_NAME);
113
114    /** Indicates that a project is invisible in the workplace. */
115    public static final int PROJECT_FLAG_HIDDEN = 4;
116
117    /** Indicates that a normal project. */
118    public static final int PROJECT_FLAG_NONE = 0;
119
120    /** Indicates a normal project. */
121    public static final CmsProjectType PROJECT_TYPE_NORMAL = CmsProjectType.MODE_PROJECT_NORMAL;
122
123    /** The project type for a workflow project. */
124    public static final CmsProjectType PROJECT_TYPE_WORKFLOW = CmsProjectType.MODE_PROJECT_WORKFLOW;
125
126    /** Indicates a temporary project that is deleted after it is published. */
127    public static final CmsProjectType PROJECT_TYPE_TEMPORARY = CmsProjectType.MODE_PROJECT_TEMPORARY;
128
129    /** The creation date of this project. */
130    private long m_dateCreated;
131
132    /** The description of this project. */
133    private String m_description;
134
135    /** The state of this project. */
136    private int m_flags;
137
138    /** The manager group id of this project. */
139    private CmsUUID m_groupManagersId;
140
141    /** The id of the user group of this project. */
142    private CmsUUID m_groupUsersId;
143
144    /** The id of this project. */
145    private CmsUUID m_id;
146
147    /** The name of this project. */
148    private String m_name;
149
150    /** The id of this projects owner. */
151    private CmsUUID m_ownerId;
152
153    /** The type of this project. */
154    private CmsProjectType m_type;
155
156    /**
157     * Default constructor for gui usage.<p>
158     */
159    public CmsProject() {
160
161        // empty
162    }
163
164    /**
165     * Creates a new CmsProject.<p>
166     *  
167     * @param projectId the id to use for this project
168     * @param projectFqn the name for this project
169     * @param description the description for this project
170     * @param ownerId the owner id for this project
171     * @param groupId the group id for this project
172     * @param managerGroupId the manager group id for this project
173     * @param flags the flags for this project
174     * @param dateCreated the creation date of this project
175     * @param type the type of this project
176     */
177    public CmsProject(
178        CmsUUID projectId,
179        String projectFqn,
180        String description,
181        CmsUUID ownerId,
182        CmsUUID groupId,
183        CmsUUID managerGroupId,
184        int flags,
185        long dateCreated,
186        CmsProjectType type) {
187
188        m_id = projectId;
189        m_name = projectFqn;
190        m_description = description;
191        m_ownerId = ownerId;
192        m_groupUsersId = groupId;
193        m_groupManagersId = managerGroupId;
194        m_flags = flags;
195        m_type = type;
196        m_dateCreated = dateCreated;
197    }
198
199    /**
200     * Throws a runtime exception if name is empty.<p>
201     * 
202     * @param name the project name to check
203     */
204    public static void checkProjectName(String name) {
205
206        if (CmsStringUtil.isEmptyOrWhitespaceOnly(name)) {
207            throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_PROJECTNAME_VALIDATION_0));
208        }
209    }
210
211    /**
212     * Checks if the full resource name (including the site root) of a resource matches
213     * any of the project resources of a project.<p>
214     * 
215     * @param projectResources a List of project resources as Strings
216     * @param resource the resource to check
217     * @return true, if the resource is "inside" the project resources
218     */
219    public static boolean isInsideProject(List<String> projectResources, CmsResource resource) {
220
221        String resourcename = resource.getRootPath();
222        return isInsideProject(projectResources, resourcename);
223    }
224
225    /**
226     * Checks if the full resource name (including the site root) of a resource matches
227     * any of the project resources of a project.<p>
228     * 
229     * @param projectResources a List of project resources as Strings
230     * @param resourcename the resource to check
231     * @return true, if the resource is "inside" the project resources
232     */
233    public static boolean isInsideProject(List<String> projectResources, String resourcename) {
234
235        for (int i = (projectResources.size() - 1); i >= 0; i--) {
236            String projectResource = projectResources.get(i);
237            if (CmsResource.isFolder(projectResource)) {
238                if (resourcename.startsWith(projectResource)) {
239                    // folder - check only the prefix
240                    return true;
241                }
242            } else {
243                if (resourcename.equals(projectResource)) {
244                    // file - check the full path
245                    return true;
246                }
247            }
248        }
249        return false;
250    }
251
252    /**
253     * Returns true if the given project id is the online project id.<p>
254     *  
255     * @param projectId the project id to check
256     * @return true if the given project id is the online project id
257     */
258    public static boolean isOnlineProject(CmsUUID projectId) {
259
260        return projectId.equals(CmsProject.ONLINE_PROJECT_ID);
261    }
262
263    /**
264     * @see java.lang.Object#clone()
265     */
266    @Override
267    public Object clone() {
268
269        return new CmsProject(
270            m_id,
271            m_name,
272            m_description,
273            m_ownerId,
274            m_groupUsersId,
275            m_groupManagersId,
276            m_flags,
277            m_dateCreated,
278            m_type);
279    }
280
281    /**
282     * Compares this instance to another given object instance of this class .<p>
283     * 
284     * @param o the other given object instance to compare with
285     * @return integer value for sorting the objects
286     */
287    public int compareTo(CmsProject o) {
288
289        if (o == this) {
290            return 0;
291        }
292
293        // compare the names
294        return m_name.compareTo(o.getName());
295    }
296
297    /**
298     * @see java.lang.Object#equals(java.lang.Object)
299     */
300    @Override
301    public boolean equals(Object obj) {
302
303        if (obj == this) {
304            return true;
305        }
306        if (obj instanceof CmsProject) {
307            return ((CmsProject)obj).m_id.equals(m_id);
308        }
309        return false;
310    }
311
312    /**
313     * Returns the creation date of this project.<p>
314     *
315     * @return the creation date of this project
316     */
317    public long getDateCreated() {
318
319        return m_dateCreated;
320    }
321
322    /**
323     * Returns the description of this project.
324     *
325     * @return the description of this project
326     */
327    public String getDescription() {
328
329        return m_description;
330    }
331
332    /**
333     * Returns the state of this project.<p>
334     *
335     * @return the state of this project
336     */
337    public int getFlags() {
338
339        return m_flags;
340    }
341
342    /**
343     * Returns the user group id of this project.<p>
344     *
345     * @return the user group id of this project
346     */
347    public CmsUUID getGroupId() {
348
349        return m_groupUsersId;
350    }
351
352    /**
353     * Returns the manager group id of this project.<p>
354     *
355     * @return the manager group id of this project
356     */
357    public CmsUUID getManagerGroupId() {
358
359        return m_groupManagersId;
360    }
361
362    /**
363     * Returns the name of this project.<p>
364     *
365     * @return the name of this project
366     */
367    public String getName() {
368
369        return m_name;
370    }
371
372    /**
373     * Returns the fully qualified name of the associated organizational unit.<p>
374     *
375     * @return the fully qualified name of the associated organizational unit
376     */
377    public String getOuFqn() {
378
379        return CmsOrganizationalUnit.getParentFqn(m_name);
380    }
381
382    /**
383     * Returns the user id of the project owner.<p>
384     *
385     * @return the user id of the project owner
386     */
387    public CmsUUID getOwnerId() {
388
389        return m_ownerId;
390    }
391
392    /**
393     * Returns the simple name of this organizational unit.
394     *
395     * @return the simple name of this organizational unit.
396     */
397    public String getSimpleName() {
398
399        return CmsOrganizationalUnit.getSimpleName(m_name);
400    }
401
402    /**
403     * Returns the type of this project.<p>
404     *
405     * @return the type of this project
406     */
407    public CmsProjectType getType() {
408
409        return m_type;
410    }
411
412    /**
413     * Returns the id of this project.<p>
414     *
415     * @return the id of this project
416     */
417    public CmsUUID getUuid() {
418
419        return m_id;
420    }
421
422    /**
423     * @see java.lang.Object#hashCode()
424     */
425    @Override
426    public int hashCode() {
427
428        if (m_name != null) {
429            return m_name.hashCode();
430        }
431        return 0;
432    }
433
434    /**
435     * Returns the delete After Publishing flag.<p>
436     *
437     * @return the delete After Publishing flag
438     * 
439     * @see #getType()
440     */
441    public boolean isDeleteAfterPublishing() {
442
443        return (m_type == CmsProject.PROJECT_TYPE_TEMPORARY);
444    }
445
446    /**
447     * Returns the 'hidden' flag.<p>
448     *
449     * @return the 'hidden' flag
450     * 
451     * @see #getFlags()
452     */
453    public boolean isHidden() {
454
455        return (getFlags() & PROJECT_FLAG_HIDDEN) == PROJECT_FLAG_HIDDEN;
456    }
457
458    /**
459     * Returns <code>true</code> if this project is the Online project.<p>
460     * 
461     * @return <code>true</code> if this project is the Online project
462     */
463    public boolean isOnlineProject() {
464
465        return isOnlineProject(m_id);
466    }
467
468    /**
469     * Returns true if this is a workflow project.<p>
470     * 
471     * @return true if this is a workflow project
472     */
473    public boolean isWorkflowProject() {
474
475        return getType().getMode() == PROJECT_TYPE_WORKFLOW.getMode();
476    }
477
478    /**
479     * Sets the delete After Publishing flag.<p>
480     *
481     * @param deleteAfterPublishing the delete After Publishing flag to set
482     */
483    public void setDeleteAfterPublishing(boolean deleteAfterPublishing) {
484
485        m_type = deleteAfterPublishing ? CmsProject.PROJECT_TYPE_TEMPORARY : CmsProject.PROJECT_TYPE_NORMAL;
486    }
487
488    /**
489     * Sets the description of this project.<p>
490     *
491     * @param description the description to set
492     */
493    public void setDescription(String description) {
494
495        m_description = description;
496    }
497
498    /**
499     * Sets the flags of this project.<p>
500     *
501     * @param flags the flag to set
502     */
503    public void setFlags(int flags) {
504
505        m_flags = flags;
506    }
507
508    /**
509     * Sets the user group id of this project.<p>
510     *
511     * @param id the user group id of this project
512     */
513    public void setGroupId(CmsUUID id) {
514
515        CmsUUID.checkId(id, false);
516        m_groupUsersId = id;
517    }
518
519    /**
520     * Sets the 'hidden' flag.<p>
521     * 
522     * @param value the value to set
523     */
524    public void setHidden(boolean value) {
525
526        if (isHidden() != value) {
527            setFlags(getFlags() ^ PROJECT_FLAG_HIDDEN);
528        }
529    }
530
531    /**
532     * Sets the manager group id of this project.<p>
533     *
534     * @param id the manager group id of this project
535     */
536    public void setManagerGroupId(CmsUUID id) {
537
538        CmsUUID.checkId(id, false);
539        m_groupManagersId = id;
540    }
541
542    /**
543     * Sets the name.<p>
544     *
545     * @param name the name to set
546     */
547    public void setName(String name) {
548
549        checkProjectName(name);
550        m_name = name;
551    }
552
553    /**
554     * Sets the owner id of this project.<p>
555     * 
556     * @param id the id of the new owner
557     */
558    public void setOwnerId(CmsUUID id) {
559
560        CmsUUID.checkId(id, false);
561        m_ownerId = id;
562    }
563
564    /**
565     * @see java.lang.Object#toString()
566     */
567    @Override
568    public String toString() {
569
570        StringBuffer result = new StringBuffer();
571        result.append("[Project]:");
572        result.append(m_name);
573        result.append(" , Id=");
574        result.append(m_id);
575        result.append(", Desc=");
576        result.append(m_description);
577        return result.toString();
578    }
579
580    /**
581     * Sets the type of this project.<p>
582     *
583     * @param type the type to set
584     */
585    void setType(CmsProjectType type) {
586
587        m_type = type;
588    }
589}