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