001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id: DocumentViewImpl.java 28460 2008-01-03 15:34:05Z sfermigier $
018 */
019
020package org.nuxeo.ecm.platform.url;
021
022import java.io.Serializable;
023import java.util.Collections;
024import java.util.HashMap;
025import java.util.Map;
026
027import org.nuxeo.ecm.core.api.DocumentLocation;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.impl.DocumentLocationImpl;
030import org.nuxeo.ecm.platform.types.adapter.TypeInfo;
031import org.nuxeo.ecm.platform.url.api.DocumentView;
032
033/**
034 * TODO: document me.
035 *
036 * @author tiry
037 */
038public class DocumentViewImpl implements DocumentView, Serializable {
039
040    private static final long serialVersionUID = 1L;
041
042    private DocumentLocation documentLocation;
043
044    private String viewId;
045
046    private String tabId;
047
048    private String subURI;
049
050    private String patternName;
051
052    private Map<String, String> parameters;
053
054    public DocumentViewImpl(DocumentLocation documentLocation, String viewId) {
055        this.documentLocation = documentLocation;
056        this.viewId = viewId;
057    }
058
059    public DocumentViewImpl(DocumentLocation documentLocation, String viewId, Map<String, String> parameters) {
060        this.documentLocation = documentLocation;
061        this.viewId = viewId;
062        this.parameters = parameters;
063    }
064
065    // Not used. Deprecate? please keep: this is the most complete constructor.
066    public DocumentViewImpl(DocumentLocation documentLocation, String viewId, String subURI,
067            Map<String, String> parameters) {
068        this.documentLocation = documentLocation;
069        this.viewId = viewId;
070        this.subURI = subURI;
071        this.parameters = parameters;
072    }
073
074    public DocumentViewImpl(DocumentModel doc) {
075        documentLocation = new DocumentLocationImpl(doc);
076        TypeInfo typeInfo = doc.getAdapter(TypeInfo.class);
077        viewId = typeInfo.getDefaultView();
078    }
079
080    @Deprecated
081    public DocumentViewImpl(DocumentLocation docLoc, String viewId, String tabId, String subURI) {
082        documentLocation = docLoc;
083        this.viewId = viewId;
084        this.tabId = tabId;
085        if (subURI != null) {
086            subURI = subURI.trim();
087            if (subURI.length() == 0) {
088                subURI = null;
089            }
090        }
091        this.subURI = subURI;
092    }
093
094    public DocumentViewImpl(DocumentLocation docLoc) {
095        documentLocation = docLoc;
096        subURI = null;
097    }
098
099    public DocumentLocation getDocumentLocation() {
100        return documentLocation;
101    }
102
103    public String getTabId() {
104        if (tabId == null && parameters != null) {
105            return parameters.get("tabId");
106        }
107        return tabId;
108    }
109
110    public String getViewId() {
111        if (viewId == null && parameters != null) {
112            return parameters.get("viewId");
113        }
114        return viewId;
115    }
116
117    public String getSubURI() {
118        return subURI;
119    }
120
121    public Map<String, String> getParameters() {
122        if (parameters == null) {
123            parameters = new HashMap<String, String>();
124        }
125        String tabId = getTabId();
126        if (tabId != null) {
127            parameters.put("tabId", tabId);
128        }
129        return Collections.unmodifiableMap(parameters);
130    }
131
132    public String getParameter(String name) {
133        if (parameters == null) {
134            return null;
135        }
136        return parameters.get(name);
137    }
138
139    public void addParameter(String name, String value) {
140        if (parameters == null) {
141            parameters = new HashMap<String, String>();
142        }
143        parameters.put(name, value);
144    }
145
146    public void removeParameter(String name) {
147        if (parameters == null) {
148            return;
149        }
150        parameters.remove(name);
151    }
152
153    public void setDocumentLocation(DocumentLocation documentLocation) {
154        this.documentLocation = documentLocation;
155    }
156
157    public void setSubURI(String subURI) {
158        this.subURI = subURI;
159    }
160
161    public void setViewId(String viewId) {
162        this.viewId = viewId;
163    }
164
165    public String getPatternName() {
166        return patternName;
167    }
168
169    public void setPatternName(String patternName) {
170        this.patternName = patternName;
171    }
172
173    @Override
174    public String toString() {
175        return String.format("DocumentViewImpl [documentLocation=%s, "
176                + "parameters=%s, patternName=%s, subURI=%s, tabId=%s, viewId=%s]", documentLocation, parameters,
177                patternName, subURI, tabId, viewId);
178    }
179
180}