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 tabId;
046
047    private String subURI;
048
049    private String patternName;
050
051    private Map<String, String> parameters;
052
053    public DocumentViewImpl(DocumentLocation documentLocation, String viewId) {
054        this.documentLocation = documentLocation;
055        this.viewId = viewId;
056    }
057
058    public DocumentViewImpl(DocumentLocation documentLocation, String viewId, Map<String, String> parameters) {
059        this.documentLocation = documentLocation;
060        this.viewId = viewId;
061        this.parameters = parameters;
062    }
063
064    // Not used. Deprecate? please keep: this is the most complete constructor.
065    public DocumentViewImpl(DocumentLocation documentLocation, String viewId, String subURI,
066            Map<String, String> parameters) {
067        this.documentLocation = documentLocation;
068        this.viewId = viewId;
069        this.subURI = subURI;
070        this.parameters = parameters;
071    }
072
073    public DocumentViewImpl(DocumentModel doc) {
074        documentLocation = new DocumentLocationImpl(doc);
075        TypeInfo typeInfo = doc.getAdapter(TypeInfo.class);
076        viewId = typeInfo.getDefaultView();
077    }
078
079    public DocumentViewImpl(DocumentLocation docLoc) {
080        documentLocation = docLoc;
081        subURI = null;
082    }
083
084    public DocumentLocation getDocumentLocation() {
085        return documentLocation;
086    }
087
088    public String getTabId() {
089        if (tabId == null && parameters != null) {
090            return parameters.get("tabId");
091        }
092        return tabId;
093    }
094
095    public String getViewId() {
096        if (viewId == null && parameters != null) {
097            return parameters.get("viewId");
098        }
099        return viewId;
100    }
101
102    public String getSubURI() {
103        return subURI;
104    }
105
106    public Map<String, String> getParameters() {
107        if (parameters == null) {
108            parameters = new HashMap<>();
109        }
110        String tabId = getTabId();
111        if (tabId != null) {
112            parameters.put("tabId", tabId);
113        }
114        return Collections.unmodifiableMap(parameters);
115    }
116
117    public String getParameter(String name) {
118        if (parameters == null) {
119            return null;
120        }
121        return parameters.get(name);
122    }
123
124    public void addParameter(String name, String value) {
125        if (parameters == null) {
126            parameters = new HashMap<>();
127        }
128        parameters.put(name, value);
129    }
130
131    public void removeParameter(String name) {
132        if (parameters == null) {
133            return;
134        }
135        parameters.remove(name);
136    }
137
138    public void setDocumentLocation(DocumentLocation documentLocation) {
139        this.documentLocation = documentLocation;
140    }
141
142    public void setSubURI(String subURI) {
143        this.subURI = subURI;
144    }
145
146    public void setViewId(String viewId) {
147        this.viewId = viewId;
148    }
149
150    public String getPatternName() {
151        return patternName;
152    }
153
154    public void setPatternName(String patternName) {
155        this.patternName = patternName;
156    }
157
158    @Override
159    public String toString() {
160        return String.format(
161                "DocumentViewImpl [documentLocation=%s, "
162                        + "parameters=%s, patternName=%s, subURI=%s, tabId=%s, viewId=%s]",
163                documentLocation, parameters, patternName, subURI, tabId, viewId);
164    }
165
166}