001/*
002 * (C) Copyright 2011 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 */
017/*
018 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and contributors.
019 *
020 * All rights reserved. This program and the accompanying materials
021 * are made available under the terms of the GNU Lesser General Public License
022 * (LGPL) version 2.1 which accompanies this distribution, and is available at
023 * http://www.gnu.org/licenses/lgpl.html
024 *
025 * This library is distributed in the hope that it will be useful,
026 * but WITHOUT ANY WARRANTY; without even the implied warranty of
027 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
028 * Lesser General Public License for more details.
029 *
030 * Contributors:
031 *     rlegall
032 */
033package org.nuxeo.ecm.user.center.profile.listeners;
034
035import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.BEFORE_DOC_UPDATE;
036import static org.nuxeo.ecm.user.center.profile.UserProfileConstants.USER_PROFILE_AVATAR_FIELD;
037import static org.nuxeo.ecm.user.center.profile.UserProfileConstants.USER_PROFILE_FACET;
038
039import java.io.Serializable;
040
041import org.nuxeo.ecm.core.api.Blob;
042import org.nuxeo.ecm.core.api.DocumentModel;
043import org.nuxeo.ecm.core.api.PropertyException;
044import org.nuxeo.ecm.core.event.Event;
045import org.nuxeo.ecm.core.event.EventListener;
046import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
047import org.nuxeo.ecm.platform.picture.api.ImageInfo;
048import org.nuxeo.ecm.platform.picture.api.ImagingService;
049import org.nuxeo.runtime.api.Framework;
050
051/**
052 * @author rlegall Listener to handle the maximum dimensions of the avatar picture. This listener is triggered on
053 *         DocumentEventTypes.BEFORE_DOC_UPDATE events. It verifies if the picture width is above 300 pixels and its
054 *         height above 200. In that case, the picture saved in the rich profile is a resized version of it which fits
055 *         those constrains.
056 */
057public class ResizeAvatarPictureListener implements EventListener {
058
059    protected static final int RESIZED_IMAGE_WIDTH = 300;
060
061    protected static final int RESIZED_IMAGE_HEIGHT = 200;
062
063    @Override
064    public void handleEvent(Event event) {
065
066        if (isBeforeUpdate(event)) {
067
068            DocumentEventContext ctx = (DocumentEventContext) event.getContext();
069            DocumentModel doc = ctx.getSourceDocument();
070
071            if (doc.hasFacet(USER_PROFILE_FACET)) {
072                Blob image = (Blob) doc.getPropertyValue(USER_PROFILE_AVATAR_FIELD);
073                if (image != null) {
074                    resizeAvatar(doc, image);
075                }
076            }
077        }
078    }
079
080    protected boolean isBeforeUpdate(Event event) {
081        return BEFORE_DOC_UPDATE.equals(event.getName()) && (event.getContext() instanceof DocumentEventContext);
082    }
083
084    protected void resizeAvatar(DocumentModel doc, Blob avatarImage) throws PropertyException {
085        ImagingService service = Framework.getService(ImagingService.class);
086        ImageInfo info = service.getImageInfo(avatarImage);
087        int width = info.getWidth();
088        int height = info.getHeight();
089        float wScale = (float) RESIZED_IMAGE_WIDTH / width;
090        float hscale = (float) RESIZED_IMAGE_HEIGHT / height;
091        float scale = Math.min(wScale, hscale);
092
093        if (scale < 1) {
094            avatarImage = service.resize(avatarImage, "jpg", (int) (width * scale), (int) (height * scale),
095                    info.getDepth());
096            avatarImage.setMimeType("image/jpeg");// XXX : Should be automatic
097            doc.setPropertyValue(USER_PROFILE_AVATAR_FIELD, (Serializable) avatarImage);
098        }
099    }
100
101}