001/*
002 * (C) Copyright 2012 Nuxeo SA (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 *     Olivier Grisel <ogrisel@nuxeo.com>
016 */
017package org.nuxeo.drive.operations;
018
019import java.io.IOException;
020import java.text.SimpleDateFormat;
021import java.util.Calendar;
022
023import org.apache.commons.lang.StringUtils;
024import org.nuxeo.drive.adapter.FileSystemItem;
025import org.nuxeo.drive.service.impl.NuxeoDriveManagerImpl;
026import org.nuxeo.ecm.automation.OperationContext;
027import org.nuxeo.ecm.automation.core.Constants;
028import org.nuxeo.ecm.automation.core.annotations.Context;
029import org.nuxeo.ecm.automation.core.annotations.Operation;
030import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
031import org.nuxeo.ecm.automation.core.annotations.Param;
032import org.nuxeo.ecm.core.api.Blob;
033import org.nuxeo.ecm.core.api.NuxeoPrincipal;
034
035/**
036 * Generates a conflicted name for a {@link FileSystemItem} given its name, the currently authenticated user's first
037 * name and last name. Doing so as an operation make it possible to override this part without having to fork the client
038 * codebase.
039 *
040 * @author Olivier Grisel
041 */
042@Operation(id = NuxeoDriveGenerateConflictedItemName.ID, category = Constants.CAT_SERVICES, label = "Nuxeo Drive: Generate Conflicted Item Name")
043public class NuxeoDriveGenerateConflictedItemName {
044
045    public static final String ID = "NuxeoDrive.GenerateConflictedItemName";
046
047    @Context
048    protected OperationContext ctx;
049
050    @Param(name = "name")
051    protected String name;
052
053    @OperationMethod
054    public Blob run() throws IOException {
055
056        String extension = "";
057        if (name.contains(".")) {
058            // Split on the last occurrence of . using a negative lookahead
059            // regexp.
060            String[] parts = name.split("\\.(?=[^\\.]+$)");
061            name = parts[0];
062            extension = "." + parts[1];
063        }
064        NuxeoPrincipal principal = (NuxeoPrincipal) ctx.getPrincipal();
065        String userName = principal.getName(); // fallback
066        if (!StringUtils.isBlank(principal.getLastName()) && !StringUtils.isBlank(principal.getFirstName())) {
067            // build more user friendly name from user info
068            userName = principal.getFirstName() + " " + principal.getLastName();
069        }
070        Calendar userDate = Calendar.getInstance(NuxeoDriveManagerImpl.UTC);
071
072        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh-mm");
073        dateFormat.setCalendar(userDate);
074        String formatedDate = dateFormat.format(userDate.getTime());
075        String contextSection = String.format(" (%s - %s)", userName, formatedDate);
076        String conflictedName = name + contextSection + extension;
077        return NuxeoDriveOperationHelper.asJSONBlob(conflictedName);
078    }
079
080}