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.apache.commons.logging.LogFactory.getLog;
022import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.BEFORE_DOC_UPDATE;
023import static org.nuxeo.ecm.user.center.profile.UserProfileConstants.USER_PROFILE_AVATAR_FIELD;
024import static org.nuxeo.ecm.user.center.profile.UserProfileConstants.USER_PROFILE_FACET;
025
026import java.io.Serializable;
027import java.util.HashMap;
028import java.util.Map;
029
030import org.apache.commons.logging.Log;
031import org.nuxeo.ecm.core.api.Blob;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.api.NuxeoException;
034import org.nuxeo.ecm.core.api.PropertyException;
035import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
036import org.nuxeo.ecm.core.api.blobholder.SimpleBlobHolder;
037import org.nuxeo.ecm.core.convert.api.ConversionService;
038import org.nuxeo.ecm.core.event.Event;
039import org.nuxeo.ecm.core.event.EventListener;
040import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
041import org.nuxeo.runtime.api.Framework;
042
043/**
044 * @author rlegall Listener to handle the maximum dimensions of the avatar picture. This listener is triggered on
045 *         DocumentEventTypes.BEFORE_DOC_UPDATE events. It verifies if the picture width is above 300 pixels and its
046 *         height above 200. In that case, the picture saved in the rich profile is a resized version of it which fits
047 *         those constrains.
048 */
049public class ResizeAvatarPictureListener implements EventListener {
050
051    protected static final int RESIZED_IMAGE_WIDTH = 300;
052
053    protected static final int RESIZED_IMAGE_HEIGHT = 200;
054
055    private static final Log log = getLog(ResizeAvatarPictureListener.class);
056
057    @Override
058    public void handleEvent(Event event) {
059
060        if (isBeforeUpdate(event)) {
061
062            DocumentEventContext ctx = (DocumentEventContext) event.getContext();
063            DocumentModel doc = ctx.getSourceDocument();
064
065            if (doc.hasFacet(USER_PROFILE_FACET)) {
066                Blob image = (Blob) doc.getPropertyValue(USER_PROFILE_AVATAR_FIELD);
067                if (image != null) {
068                    resizeAvatar(doc, image);
069                }
070            }
071        }
072    }
073
074    protected boolean isBeforeUpdate(Event event) {
075        return BEFORE_DOC_UPDATE.equals(event.getName()) && (event.getContext() instanceof DocumentEventContext);
076    }
077
078    protected void resizeAvatar(DocumentModel doc, Blob avatarImage) throws PropertyException {
079        ConversionService conversionService = Framework.getService(ConversionService.class);
080        BlobHolder bh = new SimpleBlobHolder(avatarImage);
081        Map<String, Serializable> parameters = new HashMap<>();
082        parameters.put("targetWidth", String.valueOf(RESIZED_IMAGE_WIDTH));
083        parameters.put("targetHeight", String.valueOf(RESIZED_IMAGE_HEIGHT));
084
085        try {
086            BlobHolder result = conversionService.convert("resizeAvatar", bh, parameters);
087            if (result != null) {
088                Blob blob = result.getBlob();
089                blob.setFilename(avatarImage.getFilename());
090                doc.setPropertyValue(USER_PROFILE_AVATAR_FIELD, (Serializable) blob);
091            }
092        } catch (NuxeoException e) {
093            log.warn("Unable to resize avatar image");
094            log.debug(e, e);
095        }
096
097    }
098
099}