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