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    public DocumentLocation getDocumentLocation() {
083        return documentLocation;
084    }
085
086    public String getTabId() {
087        if (parameters != null) {
088            return parameters.get("tabId");
089        }
090        return null;
091    }
092
093    public String getViewId() {
094        if (viewId == null && parameters != null) {
095            return parameters.get("viewId");
096        }
097        return viewId;
098    }
099
100    public String getSubURI() {
101        return subURI;
102    }
103
104    public Map<String, String> getParameters() {
105        if (parameters == null) {
106            parameters = new HashMap<>();
107        }
108        String tabId = getTabId();
109        if (tabId != null) {
110            parameters.put("tabId", tabId);
111        }
112        return Collections.unmodifiableMap(parameters);
113    }
114
115    public String getParameter(String name) {
116        if (parameters == null) {
117            return null;
118        }
119        return parameters.get(name);
120    }
121
122    public void addParameter(String name, String value) {
123        if (parameters == null) {
124            parameters = new HashMap<>();
125        }
126        parameters.put(name, value);
127    }
128
129    public void removeParameter(String name) {
130        if (parameters == null) {
131            return;
132        }
133        parameters.remove(name);
134    }
135
136    public void setDocumentLocation(DocumentLocation documentLocation) {
137        this.documentLocation = documentLocation;
138    }
139
140    public void setSubURI(String subURI) {
141        this.subURI = subURI;
142    }
143
144    public void setViewId(String viewId) {
145        this.viewId = viewId;
146    }
147
148    public String getPatternName() {
149        return patternName;
150    }
151
152    public void setPatternName(String patternName) {
153        this.patternName = patternName;
154    }
155
156    @Override
157    public String toString() {
158        return String.format(
159                "DocumentViewImpl [documentLocation=%s, "
160                        + "parameters=%s, patternName=%s, subURI=%s, viewId=%s]",
161                documentLocation, parameters, patternName, subURI, viewId);
162    }
163
164}