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 GmbH & Co. KG, 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.workplace;
029
030import org.opencms.ade.galleries.shared.CmsGallerySearchScope;
031import org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants.SortParams;
032import org.opencms.db.CmsPublishList;
033import org.opencms.db.CmsUserSettings;
034import org.opencms.file.CmsObject;
035import org.opencms.file.CmsResource;
036import org.opencms.file.CmsUser;
037import org.opencms.file.collectors.I_CmsResourceCollector;
038import org.opencms.i18n.CmsMessageContainer;
039import org.opencms.main.CmsException;
040import org.opencms.main.CmsLog;
041import org.opencms.main.OpenCms;
042import org.opencms.util.CmsUUID;
043import org.opencms.workplace.tools.CmsToolUserData;
044
045import java.util.HashMap;
046import java.util.Map;
047
048import org.apache.commons.logging.Log;
049
050/**
051 * Object to conveniently access and modify the state of the workplace for a user,
052 * will be stored in the session of a user.<p>
053 *
054 * @since 6.0.0
055 */
056public class CmsWorkplaceSettings {
057
058    /** Log instance for this class. */
059    private static final Log LOG = CmsLog.getLog(CmsWorkplaceSettings.class);
060
061    /** The resource collector. */
062    private I_CmsResourceCollector m_collector;
063
064    /** The current site. */
065    private String m_currentSite;
066
067    /** The diaolg object. */
068    private Object m_dialogObject;
069
070    /** The error messages. */
071    private CmsMessageContainer m_errorMessage;
072
073    /** The explorer URL. */
074    private String m_explorerFlaturl;
075
076    /** The explorer mode. */
077    private String m_explorerMode;
078
079    /** The explorer page. */
080    private int m_explorerPage;
081
082    /** The explorer project filter. */
083    private String m_explorerProjectFilter;
084
085    /** The explorer project id. */
086    private CmsUUID m_explorerProjectId;
087
088    /** The explorer resource. */
089    private Map<String, String> m_explorerResource;
090
091    /** The frame URIs. */
092    private Map<String, String> m_frameUris;
093
094    /** The gallery type. */
095    private String m_galleryType;
096
097    /** The last sort order used for the gallery search results. */
098    private SortParams m_lastGalleryResultOrder;
099
100    /** The last used galleries. */
101    private Map<String, String> m_lastUsedGalleries;
102
103    /** The list object. */
104    private Object m_listObject;
105
106    /** The permission detail view. */
107    private String m_permissionDetailView;
108
109    /** The project id. */
110    private CmsUUID m_project;
111
112    /** The publish list. */
113    private CmsPublishList m_publishList;
114
115    /** The gallery search scope. */
116    private CmsGallerySearchScope m_scope;
117
118    /** The tool user data. */
119    private CmsToolUserData m_toolUserData;
120
121    /** The tree site. */
122    private Map<String, String> m_treeSite;
123
124    /** The tree type. */
125    private Map<String, String> m_treeType;
126
127    /** The user. */
128    private CmsUser m_user;
129
130    /** The user agreement accepted flag. */
131    private boolean m_userAgreementAccepted;
132
133    /** The user settings. */
134    private CmsUserSettings m_userSettings;
135
136    /** The startup view. */
137    private String m_viewStartup;
138
139    /** The view URI. */
140    private String m_viewUri;
141
142    /**
143     * Constructor, only package visible.<p>
144     */
145    CmsWorkplaceSettings() {
146
147        m_explorerPage = 1;
148        m_explorerResource = new HashMap<String, String>();
149        m_treeType = new HashMap<String, String>();
150        m_treeSite = new HashMap<String, String>();
151        m_frameUris = new HashMap<String, String>();
152        m_lastUsedGalleries = new HashMap<String, String>();
153        m_currentSite = OpenCms.getSiteManager().getDefaultSite().getSiteRoot();
154    }
155
156    /**
157     * Returns the collector object.<p>
158     *
159     * Use this mechanism for transferring a resource collector between
160     * several page instances of an interactive dialog. <p>
161     *
162     * @return the dialog object
163     */
164    public I_CmsResourceCollector getCollector() {
165
166        return m_collector;
167    }
168
169    /**
170     * Returns the dialog object.<p>
171     *
172     * Use this mechanism for transferring a complex object between
173     * several page instances of an interactive dialog. This is usually
174     * required when editing a complex object in a dialog of the "Administration" view.<p>
175     *
176     * @return the dialog object
177     */
178    public Object getDialogObject() {
179
180        return m_dialogObject;
181    }
182
183    /**
184     * Returns the error message to display in the workplace.<p>
185     *
186     * @return the error message to display in the workplace
187     */
188    public CmsMessageContainer getErrorMessage() {
189
190        return m_errorMessage;
191    }
192
193    /**
194     * Returns the explorer flat url.<p>
195     *
196     * @return the explorer flat url
197     */
198    public String getExplorerFlaturl() {
199
200        return m_explorerFlaturl;
201    }
202
203    /**
204     * Returns the current explorer mode.<p>
205     *
206     * @return the current explorer mode
207     */
208    public String getExplorerMode() {
209
210        return m_explorerMode;
211    }
212
213    /**
214     * Returns the currently selected page in the explorer view.<p>
215     *
216     * @return the currently selected page in the explorer view
217     */
218    public int getExplorerPage() {
219
220        return m_explorerPage;
221    }
222
223    /**
224     * Gets the explorer project filter for the project view.<p>
225     *
226     * This parameter is used in the administration to filter
227     * files belonging to a project.
228     *
229     * @return the explorer project filter
230     */
231    public String getExplorerProjectFilter() {
232
233        return m_explorerProjectFilter;
234    }
235
236    /**
237     * Gets the explorer project id for the project view.<p>
238     *
239     * This parameter is used in the administration to filter
240     * files belonging to a selected project.
241     *
242     * @return the explorer project id
243     */
244    public CmsUUID getExplorerProjectId() {
245
246        return m_explorerProjectId;
247    }
248
249    /**
250     * Returns the current resource to be displayed in the explorer.<p>
251     *
252     * @return the current resource to be displayed in the explorer
253     */
254    public String getExplorerResource() {
255
256        // get the current explorer mode
257        String mode = getExplorerMode();
258        if (mode == null) {
259            mode = CmsWorkplace.VIEW_EXPLORER;
260        }
261        if (CmsWorkplace.VIEW_EXPLORER.equals(mode)) {
262            // append the current site to the key when in explorer view mode
263            mode += "_" + getSite() + "/";
264        }
265        // get the resource for the given mode
266        String resource = m_explorerResource.get(mode);
267        if (resource == null) {
268            resource = "/";
269        }
270        return resource;
271    }
272
273    /**
274     * Returns the frame URIs of the currently loaded frameset, with the frame names as keys.<p>
275     *
276     * @return the frame URIs of the currently loaded frameset
277     */
278    public Map<String, String> getFrameUris() {
279
280        return m_frameUris;
281    }
282
283    /**
284     * Returns the current gallery type name.<p>
285     *
286     * @return the current gallery type name
287     */
288    public String getGalleryType() {
289
290        return m_galleryType;
291    }
292
293    /**
294     * Gets the last result sort order for the gallery dialog.<p>
295     *
296     * @return the last sort order
297     */
298    public SortParams getLastGalleryResultOrder() {
299
300        if (m_lastGalleryResultOrder == null) {
301            return SortParams.dateLastModified_desc;
302        }
303        return m_lastGalleryResultOrder;
304    }
305
306    /**
307     * Returns the last gallery search scope.<p>
308     *
309     * @return the last gallery search scope
310     */
311    public CmsGallerySearchScope getLastSearchScope() {
312
313        if (m_scope == null) {
314            return OpenCms.getWorkplaceManager().getGalleryDefaultScope();
315        }
316        return m_scope;
317    }
318
319    /**
320     * Returns the last saved gallery for the given gallery key.<p>
321     *
322     * @param galleryKey the key for which to look up the gallery
323     * @return the last saved gallery for the given gallery key
324     **/
325    public String getLastUsedGallery(String galleryKey) {
326
327        String result = m_lastUsedGalleries.get(galleryKey);
328        LOG.info("user=" + m_user.getName() + ": getLastUsedGallery " + galleryKey + " : returning " + result);
329        return result;
330    }
331
332    /**
333     * Returns the list dialog object.<p>
334     *
335     * Use this mechanism for transfering a html list object between
336     * several page instances of an interactive dialog. This is usually
337     * required when having several lists in a tool or when a list action
338     * another list displays of the "Administration" view.<p>
339     *
340     * @return the dialog object
341     *
342     * @see org.opencms.workplace.list.A_CmsListDialog#getListObject(Class, CmsWorkplaceSettings)
343     */
344    public Object getListObject() {
345
346        return m_listObject;
347    }
348
349    /**
350     * Returns the current detail grade of the view.<p>
351     *
352     * @return value of the details.
353     */
354    public String getPermissionDetailView() {
355
356        return m_permissionDetailView;
357    }
358
359    /**
360     * Returns the currently selected project of the workplace user.<p>
361     *
362     * @return the currently selected project of the workplace user
363     */
364    public CmsUUID getProject() {
365
366        return m_project;
367    }
368
369    /**
370     * Returns the publish list.<p>
371     *
372     * @return the publishList
373     */
374    public CmsPublishList getPublishList() {
375
376        return m_publishList;
377    }
378
379    /**
380     * Returns the current site for the user.<p>
381     *
382     * @return the current site for the user
383     */
384    public String getSite() {
385
386        return m_currentSite;
387    }
388
389    /**
390     * Returns the new admin view tool User Data.<p>
391     *
392     * @return the tool User Data
393     */
394    public CmsToolUserData getToolUserData() {
395
396        return m_toolUserData;
397    }
398
399    /**
400     * Returns the tree resource uri for the specified tree type.<p>
401     *
402     * @param type the type of the tree
403     * @return the tree resource uri for the specified tree type
404     */
405    public String getTreeResource(String type) {
406
407        String result = m_treeType.get(type);
408        if (result == null) {
409            result = "/";
410        }
411        return result;
412    }
413
414    /**
415     * Returns the tree site uri for the specified tree type.<p>
416     *
417     * @param type the type of the tree
418     * @return the tree site uri for the specified tree type
419     */
420    public String getTreeSite(String type) {
421
422        String result = m_treeSite.get(type);
423        return result;
424    }
425
426    /**
427     * Returns the current workplace user.<p>
428     *
429     * @return the current workplace user
430     */
431    public CmsUser getUser() {
432
433        return m_user;
434    }
435
436    /**
437     * Returns the current workplace user settings object.<p>
438     *
439     * @return the current workplace user settings object
440     */
441    public CmsUserSettings getUserSettings() {
442
443        return m_userSettings;
444    }
445
446    /**
447     * Returns the view startup page.<p>
448     *
449     * The view startup page can be used to directly load a specific workplace dialog or other workplace resource in the
450     * OpenCms workplace base frameset after the user logs in.<p>
451     *
452     * @return the view startup page
453     */
454    public String getViewStartup() {
455
456        return m_viewStartup;
457    }
458
459    /**
460     * Returns the current view Uri selected in the workplace.<p>
461     *
462     * @return the current view Uri selected in the workplace
463     */
464    public String getViewUri() {
465
466        return m_viewUri;
467    }
468
469    /**
470     * Returns if the user agreement has been accepted in the current workplace session.<p>
471     *
472     * @return <code>true</code> if the user agreement has been accepted in the current workplace session, otherwise <code>false</code>
473     */
474    public boolean isUserAgreementAccepted() {
475
476        return m_userAgreementAccepted;
477    }
478
479    /**
480     * Checks if the current view is the explorer view.<p>
481     *
482     * @return true if the current view is the explorer view, otherwise false
483     */
484    public boolean isViewExplorer() {
485
486        return getViewUri().endsWith(CmsWorkplace.FILE_EXPLORER_FILELIST);
487    }
488
489    /**
490     * Sets the collector object.<p>
491     *
492     * Use this mechanism for transferring a resource collector between
493     * several page instances of an interactive dialog.<p>
494     *
495     * @param collector the dialog object to set
496     */
497    public void setCollector(I_CmsResourceCollector collector) {
498
499        m_collector = collector;
500    }
501
502    /**
503     * Sets the dialog object.<p>
504     *
505     * Use this mechanism for transferring a complex object between
506     * several page instances of an interactive dialog. This is usually
507     * required when editing a complex object in a dialog of the "Administration" view.<p>
508     *
509     * @param dialogObject the dialog object to set
510     */
511    public void setDialogObject(Object dialogObject) {
512
513        m_dialogObject = dialogObject;
514    }
515
516    /**
517     * Sets the error message to display in the workplace.<p>
518     *
519     * @param errorMessage the error message to display in the workplace
520     */
521    public void setErrorMessage(CmsMessageContainer errorMessage) {
522
523        m_errorMessage = errorMessage;
524    }
525
526    /**
527     * Sets the explorer flat url.<p>
528     *
529     * @param value the explorer flat url
530     */
531    public void setExplorerFlaturl(String value) {
532
533        m_explorerFlaturl = value;
534    }
535
536    /**
537     * Sets the current explorer mode.<p>
538     *
539     * @param value the current explorer mode
540     */
541    public void setExplorerMode(String value) {
542
543        m_explorerMode = value;
544    }
545
546    /**
547     * Sets the currently selected page in the explorer view.<p>
548     *
549     * @param page the currently selected page in the explorer view
550     */
551    public void setExplorerPage(int page) {
552
553        m_explorerPage = page;
554    }
555
556    /**
557     * Sets the explorer project filter for the project view.<p>
558     *
559     * @param value the explorer project filter
560     */
561    public void setExplorerProjectFilter(String value) {
562
563        m_explorerProjectFilter = value;
564    }
565
566    /**
567     * Sets the explorer project id for the project view.<p>
568     *
569     * @param value the explorer project id
570     */
571    public void setExplorerProjectId(CmsUUID value) {
572
573        m_explorerProjectId = value;
574    }
575
576    /**
577     * Sets the current resource to be displayed in the explorer.<p>
578     *
579     * @param value the current resource to be displayed in the explorer
580     *
581     * @deprecated use {@link #setExplorerResource(String, CmsObject)} instead
582     */
583    @Deprecated
584    public void setExplorerResource(String value) {
585
586        setExplorerResource(value, null);
587    }
588
589    /**
590     * Sets the current resource to be displayed in the explorer.<p>
591     *
592     * @param value the current resource to be displayed in the explorer
593     *
594     * @param cms needed for validation / normalization of the given path
595     */
596    public void setExplorerResource(String value, CmsObject cms) {
597
598        if (value == null) {
599            return;
600        }
601        // get the current explorer mode
602        String mode = getExplorerMode();
603        if (mode == null) {
604            mode = CmsWorkplace.VIEW_EXPLORER;
605        }
606        if (CmsWorkplace.VIEW_EXPLORER.equals(mode)) {
607            // append the current site to the key when in explorer view mode
608            mode += "_" + getSite() + "/";
609        }
610
611        // set the resource for the given mode
612        if (value.startsWith(CmsResource.VFS_FOLDER_SYSTEM + "/")
613            && (!value.startsWith(m_currentSite))
614            && (!CmsWorkplace.VIEW_GALLERY.equals(getExplorerMode()))) {
615            // restrict access to /system/
616            m_explorerResource.put(mode, "/");
617        } else {
618            if (cms != null) {
619                // Validation with read resource has 2 advantages:
620                // 1: Normalization of the path: a missing trailing slash is not fatal.
621                // 2: existence is verified.
622                try {
623                    CmsResource resource = cms.readResource(value);
624                    value = cms.getSitePath(resource);
625                } catch (CmsException cme) {
626                    // nop
627                }
628            }
629            m_explorerResource.put(mode, value);
630        }
631    }
632
633    /**
634     * Sets the current gallery type name.<p>
635     *
636     * @param currentGallery the current gallery type name to set
637     */
638    public void setGalleryType(String currentGallery) {
639
640        m_galleryType = currentGallery;
641    }
642
643    /**
644     * Sets the last sort order used for the gallery results.<p>
645     *
646     * @param order the last sort order
647     */
648    public void setLastGalleryResultOrder(SortParams order) {
649
650        m_lastGalleryResultOrder = order;
651    }
652
653    /**
654     * Sets the last gallery search scope.<p>
655     *
656     * @param scope the gallery search scope
657     */
658    public void setLastSearchScope(CmsGallerySearchScope scope) {
659
660        m_scope = scope;
661    }
662
663    /**
664     * Saves the last gallery for a given key.<p>
665     *
666     * @param galleryKey the gallery key
667     * @param gallerypath the resourcepath of the gallery
668     */
669    public void setLastUsedGallery(String galleryKey, String gallerypath) {
670
671        m_lastUsedGalleries.put(galleryKey, gallerypath);
672        LOG.info("user=" + m_user.getName() + ": setLastUsedGallery " + galleryKey + " -> " + gallerypath);
673    }
674
675    /**
676     * Sets the list object.<p>
677     *
678     * Use this mechanism for transfering a html list object between
679     * several page instances of an interactive dialog. This is usually
680     * required when having several lists in a tool or when a list action
681     * another list displays of the "Administration" view.<p>
682     *
683     * @param listObject the list object to set
684     *
685     * @see org.opencms.workplace.list.A_CmsListDialog#setListObject(Class, org.opencms.workplace.list.CmsHtmlList)
686     */
687    public void setListObject(Object listObject) {
688
689        m_listObject = listObject;
690    }
691
692    /**
693     * Sets the current detail grade of the view.<p>
694     *
695     * @param value the current details.
696     */
697    public void setPermissionDetailView(String value) {
698
699        m_permissionDetailView = value;
700    }
701
702    /**
703     * Sets the currently selected project of the workplace user.<p>
704     *
705     * @param project the currently selected project of thw workplace user
706     */
707    public void setProject(CmsUUID project) {
708
709        m_project = project;
710    }
711
712    /**
713     * Sets the publish list.<p>
714     *
715     * @param publishList the publishList to set
716     */
717    public void setPublishList(CmsPublishList publishList) {
718
719        m_publishList = publishList;
720    }
721
722    /**
723     * Sets the current site for the user.<p>
724     *
725     * @param value the current site for the user
726     */
727    public void setSite(String value) {
728
729        if ((value != null) && !value.equals(m_currentSite)) {
730            m_currentSite = value;
731            m_treeType = new HashMap<String, String>();
732        }
733    }
734
735    /**
736     * Sets the new admin view tool User Data.<p>
737     *
738     * @param toolUserData the tool User Data to set
739     */
740    public void setToolUserData(CmsToolUserData toolUserData) {
741
742        m_toolUserData = toolUserData;
743    }
744
745    /**
746     * Sets the tree resource uri for the specified tree type.<p>
747     *
748     * @param type the type of the tree
749     * @param value the resource uri to set for the type
750     */
751    public void setTreeResource(String type, String value) {
752
753        if (value == null) {
754            return;
755        }
756        if (value.startsWith(CmsResource.VFS_FOLDER_SYSTEM + "/") && (!value.startsWith(m_currentSite))) {
757            // restrict access to /system/
758            value = "/";
759        }
760        m_treeType.put(type, value);
761    }
762
763    /**
764     * Sets the tree resource uri for the specified tree type.<p>
765     *
766     * @param type the type of the tree
767     * @param value the resource uri to set for the type
768     */
769    public void setTreeSite(String type, String value) {
770
771        if (value == null) {
772            return;
773        }
774        m_treeSite.put(type, value);
775    }
776
777    /**
778     * Sets the current workplace user.<p>
779     *
780     * @param user the current workplace user
781     */
782    public void setUser(CmsUser user) {
783
784        m_user = user;
785    }
786
787    /**
788     * Sets if the user agreement has been accepted in the current workplace session.<p>
789     *
790     * @param userAgreementAccepted <code>true</code> if the user agreement has been accepted in the current workplace session, otherwise <code>false</code>
791     */
792    public void setUserAgreementAccepted(boolean userAgreementAccepted) {
793
794        m_userAgreementAccepted = userAgreementAccepted;
795    }
796
797    /**
798     * Sets the current workplace user settings object.<p>
799     *
800     * @param userSettings the current workplace user settings object
801     */
802    public void setUserSettings(CmsUserSettings userSettings) {
803
804        m_userSettings = userSettings;
805    }
806
807    /**
808     * Sets the view startup page.<p>
809     *
810     * The view startup page can be used to directly load a specific workplace dialog or other workplace resource in the
811     * OpenCms workplace base frameset after the user logs in.<p>
812     *
813     * @param viewStartup the view startup page to set
814     */
815    public void setViewStartup(String viewStartup) {
816
817        m_viewStartup = viewStartup;
818    }
819
820    /**
821     * Sets the view Uri for the workplace.<p>
822     *
823     * @param string the view Uri for the workplace
824     */
825    public void setViewUri(String string) {
826
827        m_viewUri = string;
828    }
829
830}