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