001/*
002 * (C) Copyright 2012 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 *     Thierry Delprat
018 */
019package org.nuxeo.template.adapters.doc;
020
021import java.io.Serializable;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import org.nuxeo.runtime.api.Framework;
027import org.nuxeo.template.api.TemplateInput;
028import org.nuxeo.template.serializer.service.TemplateSerializerService;
029
030public class TemplateBinding {
031
032    public static final String TEMPLATE_NAME_KEY = "templateName";
033
034    public static final String TEMPLATE_DATA_KEY = "templateData";
035
036    public static final String TEMPLATE_ID_KEY = "templateId";
037
038    public static final String TEMPLATE_USE_BLOB_KEY = "useMainContentAsTemplate";
039
040    private String name;
041
042    private String templateId;
043
044    private String data;
045
046    private boolean useMainContentAsTemplate;
047
048    public TemplateBinding() {
049    }
050
051    public TemplateBinding(Map<String, Serializable> map) {
052        name = (String) map.get(TEMPLATE_NAME_KEY);
053        templateId = (String) map.get(TEMPLATE_ID_KEY);
054        data = (String) map.get(TEMPLATE_DATA_KEY);
055        if (map.get(TEMPLATE_USE_BLOB_KEY) != null) {
056            useMainContentAsTemplate = (Boolean) map.get(TEMPLATE_USE_BLOB_KEY);
057        } else {
058            useMainContentAsTemplate = false;
059        }
060    }
061
062    public String getName() {
063        return name;
064    }
065
066    public void setName(String name) {
067        this.name = name;
068    }
069
070    public String getTemplateId() {
071        return templateId;
072    }
073
074    public void setTemplateId(String templateId) {
075        this.templateId = templateId;
076    }
077
078    public String getData() {
079        return data;
080    }
081
082    public void setData(String data) {
083        this.data = data;
084    }
085
086    public void setData(List<TemplateInput> params) {
087        String xml = Framework.getService(TemplateSerializerService.class).serializeXML(params);
088        setData(xml);
089    }
090
091    public boolean isUseMainContentAsTemplate() {
092        return useMainContentAsTemplate;
093    }
094
095    public void setUseMainContentAsTemplate(boolean useMainContentAsTemplate) {
096        this.useMainContentAsTemplate = useMainContentAsTemplate;
097    }
098
099    public void update(TemplateBinding other) {
100        name = other.name;
101        templateId = other.templateId;
102        data = other.data;
103        useMainContentAsTemplate = other.useMainContentAsTemplate;
104    }
105
106    public Map<String, Serializable> getAsMap() {
107        Map<String, Serializable> map = new HashMap<>();
108        map.put(TEMPLATE_NAME_KEY, name);
109        map.put(TEMPLATE_ID_KEY, templateId);
110        map.put(TEMPLATE_DATA_KEY, data);
111        map.put(TEMPLATE_USE_BLOB_KEY, useMainContentAsTemplate);
112        return map;
113    }
114}