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