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.processors;
020
021import java.io.IOException;
022import java.io.Serializable;
023import java.util.Date;
024import java.util.List;
025import java.util.Map;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.ecm.core.api.Blob;
030import org.nuxeo.ecm.core.api.PropertyException;
031import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
032import org.nuxeo.ecm.core.api.model.Property;
033import org.nuxeo.ecm.core.schema.types.Type;
034import org.nuxeo.ecm.core.schema.types.primitives.BooleanType;
035import org.nuxeo.ecm.core.schema.types.primitives.DateType;
036import org.nuxeo.ecm.core.schema.types.primitives.StringType;
037import org.nuxeo.ecm.platform.preview.api.HtmlPreviewAdapter;
038import org.nuxeo.ecm.platform.rendering.fm.adapters.DocumentObjectWrapper;
039import org.nuxeo.template.api.ContentInputType;
040import org.nuxeo.template.api.InputType;
041import org.nuxeo.template.api.TemplateInput;
042import org.nuxeo.template.api.adapters.TemplateBasedDocument;
043
044import freemarker.template.TemplateModelException;
045
046public abstract class AbstractBindingResolver implements InputBindingResolver {
047
048    protected Log log = LogFactory.getLog(AbstractBindingResolver.class);
049
050    protected abstract Object handleLoop(String paramName, Object value);
051
052    protected abstract Object handlePictureField(String paramName, Blob blobValue);
053
054    protected abstract void handleBlobField(String paramName, Blob blobValue);
055
056    protected String handleHtmlField(String paramName, String htmlValue) {
057        return HtmlBodyExtractor.extractHtmlBody(htmlValue);
058    }
059
060    protected DocumentObjectWrapper nuxeoWrapper = new DocumentObjectWrapper(null);
061
062    public AbstractBindingResolver() {
063        super();
064    }
065
066    protected DocumentObjectWrapper getWrapper() {
067        return nuxeoWrapper;
068    }
069
070    @Override
071    public void resolve(List<TemplateInput> inputParams, Map<String, Object> context,
072            TemplateBasedDocument templateBasedDocument) {
073
074        for (TemplateInput param : inputParams) {
075            try {
076                if (param.isSourceValue()) {
077                    if (param.getType() == InputType.Content) {
078
079                        if (ContentInputType.HtmlPreview.getValue().equals(param.getSource())) {
080                            HtmlPreviewAdapter preview = templateBasedDocument.getAdaptedDoc().getAdapter(
081                                    HtmlPreviewAdapter.class);
082                            String htmlValue = "";
083                            if (preview != null) {
084                                List<Blob> blobs = preview.getFilePreviewBlobs();
085                                if (blobs.size() > 0) {
086                                    Blob htmlBlob = preview.getFilePreviewBlobs().get(0);
087                                    if (htmlBlob != null) {
088                                        htmlValue = htmlBlob.getString();
089                                    }
090                                }
091                            } else {
092                                BlobHolder bh = templateBasedDocument.getAdaptedDoc().getAdapter(BlobHolder.class);
093                                if (bh != null) {
094                                    Blob htmlBlob = bh.getBlob();
095                                    if (htmlBlob != null && htmlBlob.getMimeType() != null
096                                            && htmlBlob.getMimeType().startsWith("text/")) {
097                                        htmlValue = htmlBlob.getString();
098                                    }
099                                }
100                            }
101                            htmlValue = handleHtmlField(param.getName(), htmlValue);
102                            context.put(param.getName(), htmlValue);
103                            continue;
104                        } else if (ContentInputType.BlobContent.getValue().equals(param.getSource())) {
105                            Object propValue = templateBasedDocument.getAdaptedDoc().getPropertyValue(param.getSource());
106                            if (propValue != null && propValue instanceof Blob) {
107                                Blob blobValue = (Blob) propValue;
108                                context.put(param.getName(), blobValue.getString());
109                                handleBlobField(param.getName(), blobValue);
110                            }
111                        } else {
112                            Object propValue = templateBasedDocument.getAdaptedDoc().getPropertyValue(param.getSource());
113                            if (propValue instanceof String) {
114                                String stringContent = (String) propValue;
115                                String htmlValue = handleHtmlField(param.getName(), stringContent);
116                                context.put(param.getName(), htmlValue);
117                            }
118                        }
119                    }
120                    Property property = null;
121                    try {
122                        property = templateBasedDocument.getAdaptedDoc().getProperty(param.getSource());
123                    } catch (PropertyException e) {
124                        log.warn("Unable to ready property " + param.getSource(), e);
125                    }
126
127                    Serializable value = null;
128                    if (property != null) {
129                        value = property.getValue();
130                    }
131
132                    if (value != null) {
133                        if (param.getType() == InputType.Content) {
134
135                        } else {
136                            if (Blob.class.isAssignableFrom(value.getClass())) {
137                                Blob blob = (Blob) value;
138                                if (param.getType() == InputType.PictureProperty) {
139                                    if (blob.getMimeType() == null || "".equals(blob.getMimeType().trim())) {
140                                        blob.setMimeType("image/jpeg");
141                                    }
142                                    context.put(param.getName(), handlePictureField(param.getName(), blob));
143                                }
144                            } else {
145                                if (param.isAutoLoop()) {
146                                    // should do the same on all children
147                                    // properties ?
148                                    Object loopVal = handleLoop(param.getName(), property);
149                                    context.put(param.getName(), loopVal);
150                                } else {
151                                    context.put(param.getName(), nuxeoWrapper.wrap(property));
152                                }
153                            }
154                        }
155                    } else {
156                        // no available value, try to find a default one ...
157                        if (property != null) {
158                            Type pType = property.getType();
159                            if (pType.getName().equals(BooleanType.ID)) {
160                                context.put(param.getName(), new Boolean(false));
161                            } else if (pType.getName().equals(DateType.ID)) {
162                                context.put(param.getName(), new Date());
163                            } else if (pType.getName().equals(StringType.ID)) {
164                                context.put(param.getName(), "");
165                            } else if (pType.getName().equals(InputType.Content)) {
166                                context.put(param.getName(), "");
167                            } else {
168                                context.put(param.getName(), "!NOVALUE!");
169                            }
170                            // handle special case for pictures
171                            if (param.getType() == InputType.PictureProperty) {
172                                context.put(param.getName(), handlePictureField(param.getName(), null));
173                            }
174                        } else {
175                            if (param.getType().equals(InputType.PictureProperty)) {
176                                context.put(param.getName(), handlePictureField(param.getName(), null));
177                            }
178                        }
179                    }
180
181                } else {
182                    if (InputType.StringValue.equals(param.getType())) {
183                        context.put(param.getName(), param.getStringValue());
184                    } else if (InputType.BooleanValue.equals(param.getType())) {
185                        context.put(param.getName(), param.getBooleanValue());
186                    } else if (InputType.DateValue.equals(param.getType())) {
187                        context.put(param.getName(), param.getDateValue());
188                    }
189                }
190            } catch (TemplateModelException | IOException e) {
191                log.warn("Unable to handle binding for param " + param.getName(), e);
192            }
193        }
194    }
195
196}