001/*
002 * (C) Copyright 2011 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 *     rlegall
018 */
019package org.nuxeo.ecm.user.center.profile.listeners;
020
021import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.BEFORE_DOC_UPDATE;
022import static org.nuxeo.ecm.user.center.profile.UserProfileConstants.USER_PROFILE_AVATAR_FIELD;
023import static org.nuxeo.ecm.user.center.profile.UserProfileConstants.USER_PROFILE_FACET;
024
025import java.io.Serializable;
026
027import org.nuxeo.ecm.core.api.Blob;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.PropertyException;
030import org.nuxeo.ecm.core.event.Event;
031import org.nuxeo.ecm.core.event.EventListener;
032import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
033import org.nuxeo.ecm.platform.picture.api.ImageInfo;
034import org.nuxeo.ecm.platform.picture.api.ImagingService;
035import org.nuxeo.runtime.api.Framework;
036
037/**
038 * @author rlegall Listener to handle the maximum dimensions of the avatar picture. This listener is triggered on
039 *         DocumentEventTypes.BEFORE_DOC_UPDATE events. It verifies if the picture width is above 300 pixels and its
040 *         height above 200. In that case, the picture saved in the rich profile is a resized version of it which fits
041 *         those constrains.
042 */
043public class ResizeAvatarPictureListener implements EventListener {
044
045    protected static final int RESIZED_IMAGE_WIDTH = 300;
046
047    protected static final int RESIZED_IMAGE_HEIGHT = 200;
048
049    @Override
050    public void handleEvent(Event event) {
051
052        if (isBeforeUpdate(event)) {
053
054            DocumentEventContext ctx = (DocumentEventContext) event.getContext();
055            DocumentModel doc = ctx.getSourceDocument();
056
057            if (doc.hasFacet(USER_PROFILE_FACET)) {
058                Blob image = (Blob) doc.getPropertyValue(USER_PROFILE_AVATAR_FIELD);
059                if (image != null) {
060                    resizeAvatar(doc, image);
061                }
062            }
063        }
064    }
065
066    protected boolean isBeforeUpdate(Event event) {
067        return BEFORE_DOC_UPDATE.equals(event.getName()) && (event.getContext() instanceof DocumentEventContext);
068    }
069
070    protected void resizeAvatar(DocumentModel doc, Blob avatarImage) throws PropertyException {
071        ImagingService service = Framework.getService(ImagingService.class);
072        ImageInfo info = service.getImageInfo(avatarImage);
073        int width = info.getWidth();
074        int height = info.getHeight();
075        float wScale = (float) RESIZED_IMAGE_WIDTH / width;
076        float hscale = (float) RESIZED_IMAGE_HEIGHT / height;
077        float scale = Math.min(wScale, hscale);
078
079        if (scale < 1) {
080            avatarImage = service.resize(avatarImage, "jpg", (int) (width * scale), (int) (height * scale),
081                    info.getDepth());
082            avatarImage.setMimeType("image/jpeg");// XXX : Should be automatic
083            doc.setPropertyValue(USER_PROFILE_AVATAR_FIELD, (Serializable) avatarImage);
084        }
085    }
086
087}