001/*
002 * (C) Copyright 2006-2007 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 *
019 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
020 */
021
022package org.nuxeo.ecm.platform.picture.api.adapters;
023
024import static org.nuxeo.ecm.platform.picture.api.ImagingDocumentConstants.PICTURE_VIEWS_PROPERTY;
025
026import java.io.Serializable;
027import java.util.ArrayList;
028import java.util.Collection;
029import java.util.HashMap;
030import java.util.List;
031import java.util.Map;
032
033import org.nuxeo.ecm.core.api.Blob;
034import org.nuxeo.ecm.core.api.DocumentModel;
035import org.nuxeo.ecm.platform.picture.api.ImageInfo;
036import org.nuxeo.ecm.platform.picture.api.PictureView;
037import org.nuxeo.ecm.platform.picture.api.PictureViewImpl;
038
039public class MultiviewPictureAdapter implements MultiviewPicture {
040
041    public static final String FIELD_HEADLINE = "headline";
042
043    public static final String FIELD_SUBHEADLINE = "subheadline";
044
045    public static final String FIELD_BYLINE = "byline";
046
047    public static final String FIELD_DATELINE = "dateline";
048
049    public static final String FIELD_SLUGLINE = "slugline";
050
051    public static final String FIELD_CREDIT = "credit";
052
053    public static final String FIELD_LANGUAGE = "language";
054
055    public static final String FIELD_SOURCE = "source";
056
057    public static final String FIELD_ORIGIN = "origin";
058
059    public static final String FIELD_GENRE = "genre";
060
061    public static final String FIELD_CAPTION = "caption";
062
063    public static final String FIELD_TYPAGE = "typage";
064
065    public static final String FIELD_INFO = "info";
066
067    public static final String SCHEMA_NAME = "picture";
068
069    final DocumentModel docModel;
070
071    final Map<String, PictureView> views = new HashMap<String, PictureView>();
072
073    public static Map<String, Object> viewToMap(PictureView view) {
074        Map<String, Object> map = new HashMap<String, Object>();
075        map.put(PictureView.FIELD_TITLE, view.getTitle());
076        map.put(PictureView.FIELD_DESCRIPTION, view.getDescription());
077        map.put(PictureView.FIELD_TAG, view.getTag());
078        map.put(PictureView.FIELD_HEIGHT, view.getHeight());
079        map.put(PictureView.FIELD_WIDTH, view.getWidth());
080        map.put(PictureView.FIELD_FILENAME, view.getFilename());
081        Blob blob = view.getBlob();
082        if (blob != null) {
083            map.put(PictureView.FIELD_CONTENT, blob);
084        }
085        map.put(FIELD_INFO, view.getImageInfo().toMap());
086        return map;
087    }
088
089    public static PictureView mapToView(Map<String, Object> map) {
090        PictureView view = new PictureViewImpl();
091        view.setTitle((String) map.get(PictureView.FIELD_TITLE));
092        view.setDescription((String) map.get(PictureView.FIELD_DESCRIPTION));
093        view.setTag((String) map.get(PictureView.FIELD_TAG));
094        Object o = map.get(PictureView.FIELD_WIDTH);
095        if (o != null) {
096            view.setWidth(((Number) o).intValue());
097        }
098        o = map.get(PictureView.FIELD_HEIGHT);
099        if (o != null) {
100            view.setHeight(((Number) o).intValue());
101        }
102        view.setFilename((String) map.get(PictureView.FIELD_FILENAME));
103        Blob blob = (Blob) map.get(PictureView.FIELD_CONTENT);
104        view.setBlob(blob);
105        view.setImageInfo(ImageInfo.fromMap((Map<String, Serializable>) map.get(FIELD_INFO)));
106        return view;
107    }
108
109    @SuppressWarnings("unchecked")
110    public MultiviewPictureAdapter(DocumentModel docModel) {
111        this.docModel = docModel;
112
113        List<Map<String, Object>> list = (List<Map<String, Object>>) docModel.getPropertyValue(PICTURE_VIEWS_PROPERTY);
114        if (list != null) {
115            for (Map<String, Object> map : list) {
116                PictureView view = mapToView(map);
117                views.put(view.getTitle(), view);
118            }
119        }
120    }
121
122    @Override
123    public PictureView[] getViews() {
124        Collection<PictureView> collection = views.values();
125        return collection.toArray(new PictureView[collection.size()]);
126    }
127
128    @Override
129    public PictureView getView(String title) {
130        return views.get(title);
131    }
132
133    @Override
134    public void removeView(String name) {
135        views.remove(name);
136        List<Map<String, Object>> v = new ArrayList<Map<String, Object>>();
137        for (PictureView view : views.values()) {
138            v.add(viewToMap(view));
139        }
140        docModel.setPropertyValue(PICTURE_VIEWS_PROPERTY, v.toArray());
141    }
142
143    @Override
144    public void addView(PictureView view) {
145        views.put(view.getTitle(), view);
146        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
147        for (PictureView pv : views.values()) {
148            list.add(viewToMap(pv));
149        }
150        docModel.setPropertyValue(PICTURE_VIEWS_PROPERTY, (Serializable) list);
151    }
152
153    @Override
154    public void removeAllView() {
155        docModel.setPropertyValue(PICTURE_VIEWS_PROPERTY, null);
156        views.clear();
157    }
158
159    @Override
160    public String getHeadline() {
161        return (String) docModel.getProperty(SCHEMA_NAME, FIELD_HEADLINE);
162    }
163
164    @Override
165    public void setHeadline(String headline) {
166        docModel.setProperty(SCHEMA_NAME, FIELD_HEADLINE, headline);
167    }
168
169    @Override
170    public String getSubheadline() {
171        return (String) docModel.getProperty(SCHEMA_NAME, FIELD_SUBHEADLINE);
172    }
173
174    @Override
175    public void setSubheadline(String subheadline) {
176        docModel.setProperty(SCHEMA_NAME, FIELD_SUBHEADLINE, subheadline);
177    }
178
179    @Override
180    public String getByline() {
181        return (String) docModel.getProperty(SCHEMA_NAME, FIELD_BYLINE);
182    }
183
184    @Override
185    public void setByline(String byline) {
186        docModel.setProperty(SCHEMA_NAME, FIELD_BYLINE, byline);
187    }
188
189    @Override
190    public String getDateline() {
191        return (String) docModel.getProperty(SCHEMA_NAME, FIELD_DATELINE);
192    }
193
194    @Override
195    public void setDateline(String dateline) {
196        docModel.setProperty(SCHEMA_NAME, FIELD_DATELINE, dateline);
197    }
198
199    @Override
200    public String getSlugline() {
201        return (String) docModel.getProperty(SCHEMA_NAME, FIELD_SLUGLINE);
202    }
203
204    @Override
205    public void setSlugline(String slugline) {
206        docModel.setProperty(SCHEMA_NAME, FIELD_SLUGLINE, slugline);
207    }
208
209    @Override
210    public String getCredit() {
211        return (String) docModel.getProperty(SCHEMA_NAME, FIELD_CREDIT);
212    }
213
214    @Override
215    public void setCredit(String credit) {
216        docModel.setProperty(SCHEMA_NAME, FIELD_CREDIT, credit);
217    }
218
219    @Override
220    public String getLanguage() {
221        return (String) docModel.getProperty(SCHEMA_NAME, FIELD_LANGUAGE);
222    }
223
224    @Override
225    public void setLanguage(String language) {
226        docModel.setProperty(SCHEMA_NAME, FIELD_LANGUAGE, language);
227    }
228
229    @Override
230    public String getSource() {
231        return (String) docModel.getProperty(SCHEMA_NAME, FIELD_SOURCE);
232    }
233
234    @Override
235    public void setSource(String source) {
236        docModel.setProperty(SCHEMA_NAME, FIELD_SOURCE, source);
237    }
238
239    @Override
240    public String getOrigin() {
241        return (String) docModel.getProperty(SCHEMA_NAME, FIELD_ORIGIN);
242    }
243
244    @Override
245    public void setOrigin(String origin) {
246        docModel.setProperty(SCHEMA_NAME, FIELD_ORIGIN, origin);
247    }
248
249    @Override
250    public String getGenre() {
251        return (String) docModel.getProperty(SCHEMA_NAME, FIELD_GENRE);
252    }
253
254    @Override
255    public void setGenre(String genre) {
256        docModel.setProperty(SCHEMA_NAME, FIELD_GENRE, genre);
257    }
258
259    @Override
260    public String getCaption() {
261        return (String) docModel.getProperty(SCHEMA_NAME, FIELD_CAPTION);
262    }
263
264    @Override
265    public void setCaption(String caption) {
266        docModel.setProperty(SCHEMA_NAME, FIELD_CAPTION, caption);
267    }
268
269    @Override
270    public String getTypage() {
271        return (String) docModel.getProperty(SCHEMA_NAME, FIELD_TYPAGE);
272    }
273
274    @Override
275    public void setTypage(String typage) {
276        docModel.setProperty(SCHEMA_NAME, FIELD_TYPAGE, typage);
277    }
278
279}