001/* 002 * (C) Copyright 2012-2018 Nuxeo (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.lang3.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.Blobs; 036import org.nuxeo.ecm.core.api.NuxeoPrincipal; 037 038/** 039 * Generates a conflicted name for a {@link FileSystemItem} given its name, the currently authenticated user's first 040 * name and last name. Doing so as an operation make it possible to override this part without having to fork the client 041 * codebase. 042 * 043 * @author Olivier Grisel 044 * @deprecated since 10.3 045 */ 046@Deprecated 047@Operation(id = NuxeoDriveGenerateConflictedItemName.ID, category = Constants.CAT_SERVICES, label = "Nuxeo Drive: Generate Conflicted Item Name", description = "Generate a conflicted name for a file system item given its name, the currently authenticated user's first name and last name." // 048 + " Return the result as a JSON blob.", deprecatedSince = "10.3") 049public class NuxeoDriveGenerateConflictedItemName { 050 051 public static final String ID = "NuxeoDrive.GenerateConflictedItemName"; 052 053 @Context 054 protected OperationContext ctx; 055 056 @Param(name = "name", description = "Name from which to generate the conflicted name.") 057 protected String name; 058 059 @OperationMethod 060 public Blob run() throws IOException { 061 String extension = ""; 062 if (name.contains(".")) { 063 // Split on the last occurrence of . using a negative lookahead 064 // regexp. 065 String[] parts = name.split("\\.(?=[^\\.]+$)"); 066 name = parts[0]; 067 extension = "." + parts[1]; 068 } 069 NuxeoPrincipal principal = ctx.getPrincipal(); 070 String userName = principal.getName(); // fallback 071 if (!StringUtils.isBlank(principal.getLastName()) && !StringUtils.isBlank(principal.getFirstName())) { 072 // build more user friendly name from user info 073 userName = principal.getFirstName() + " " + principal.getLastName(); 074 } 075 Calendar userDate = Calendar.getInstance(NuxeoDriveManagerImpl.UTC); 076 077 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm"); 078 dateFormat.setCalendar(userDate); 079 String formatedDate = dateFormat.format(userDate.getTime()); 080 String contextSection = String.format(" (%s - %s)", userName, formatedDate); 081 String conflictedName = name + contextSection + extension; 082 return Blobs.createJSONBlobFromValue(conflictedName); 083 } 084 085}