001/*
002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id: DocumentViewImpl.java 28460 2008-01-03 15:34:05Z sfermigier $
020 */
021
022package org.nuxeo.ecm.platform.url;
023
024import java.io.Serializable;
025import java.util.Collections;
026import java.util.HashMap;
027import java.util.Map;
028
029import org.nuxeo.ecm.core.api.DocumentLocation;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.api.impl.DocumentLocationImpl;
032import org.nuxeo.ecm.platform.types.adapter.TypeInfo;
033import org.nuxeo.ecm.platform.url.api.DocumentView;
034
035/**
036 * TODO: document me.
037 *
038 * @author tiry
039 */
040public class DocumentViewImpl implements DocumentView, Serializable {
041
042    private static final long serialVersionUID = 1L;
043
044    private DocumentLocation documentLocation;
045
046    private String viewId;
047
048    private String tabId;
049
050    private String subURI;
051
052    private String patternName;
053
054    private Map<String, String> parameters;
055
056    public DocumentViewImpl(DocumentLocation documentLocation, String viewId) {
057        this.documentLocation = documentLocation;
058        this.viewId = viewId;
059    }
060
061    public DocumentViewImpl(DocumentLocation documentLocation, String viewId, Map<String, String> parameters) {
062        this.documentLocation = documentLocation;
063        this.viewId = viewId;
064        this.parameters = parameters;
065    }
066
067    // Not used. Deprecate? please keep: this is the most complete constructor.
068    public DocumentViewImpl(DocumentLocation documentLocation, String viewId, String subURI,
069            Map<String, String> parameters) {
070        this.documentLocation = documentLocation;
071        this.viewId = viewId;
072        this.subURI = subURI;
073        this.parameters = parameters;
074    }
075
076    public DocumentViewImpl(DocumentModel doc) {
077        documentLocation = new DocumentLocationImpl(doc);
078        TypeInfo typeInfo = doc.getAdapter(TypeInfo.class);
079        viewId = typeInfo.getDefaultView();
080    }
081
082    @Deprecated
083    public DocumentViewImpl(DocumentLocation docLoc, String viewId, String tabId, String subURI) {
084        documentLocation = docLoc;
085        this.viewId = viewId;
086        this.tabId = tabId;
087        if (subURI != null) {
088            subURI = subURI.trim();
089            if (subURI.length() == 0) {
090                subURI = null;
091            }
092        }
093        this.subURI = subURI;
094    }
095
096    public DocumentViewImpl(DocumentLocation docLoc) {
097        documentLocation = docLoc;
098        subURI = null;
099    }
100
101    public DocumentLocation getDocumentLocation() {
102        return documentLocation;
103    }
104
105    public String getTabId() {
106        if (tabId == null && parameters != null) {
107            return parameters.get("tabId");
108        }
109        return tabId;
110    }
111
112    public String getViewId() {
113        if (viewId == null && parameters != null) {
114            return parameters.get("viewId");
115        }
116        return viewId;
117    }
118
119    public String getSubURI() {
120        return subURI;
121    }
122
123    public Map<String, String> getParameters() {
124        if (parameters == null) {
125            parameters = new HashMap<String, String>();
126        }
127        String tabId = getTabId();
128        if (tabId != null) {
129            parameters.put("tabId", tabId);
130        }
131        return Collections.unmodifiableMap(parameters);
132    }
133
134    public String getParameter(String name) {
135        if (parameters == null) {
136            return null;
137        }
138        return parameters.get(name);
139    }
140
141    public void addParameter(String name, String value) {
142        if (parameters == null) {
143            parameters = new HashMap<String, String>();
144        }
145        parameters.put(name, value);
146    }
147
148    public void removeParameter(String name) {
149        if (parameters == null) {
150            return;
151        }
152        parameters.remove(name);
153    }
154
155    public void setDocumentLocation(DocumentLocation documentLocation) {
156        this.documentLocation = documentLocation;
157    }
158
159    public void setSubURI(String subURI) {
160        this.subURI = subURI;
161    }
162
163    public void setViewId(String viewId) {
164        this.viewId = viewId;
165    }
166
167    public String getPatternName() {
168        return patternName;
169    }
170
171    public void setPatternName(String patternName) {
172        this.patternName = patternName;
173    }
174
175    @Override
176    public String toString() {
177        return String.format("DocumentViewImpl [documentLocation=%s, "
178                + "parameters=%s, patternName=%s, subURI=%s, tabId=%s, viewId=%s]", documentLocation, parameters,
179                patternName, subURI, tabId, viewId);
180    }
181
182}