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.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 */ 045@Operation(id = NuxeoDriveGenerateConflictedItemName.ID, category = Constants.CAT_SERVICES, label = "Nuxeo Drive: Generate Conflicted Item Name") 046public class NuxeoDriveGenerateConflictedItemName { 047 048 public static final String ID = "NuxeoDrive.GenerateConflictedItemName"; 049 050 @Context 051 protected OperationContext ctx; 052 053 @Param(name = "name") 054 protected String name; 055 056 @OperationMethod 057 public Blob run() throws IOException { 058 059 String extension = ""; 060 if (name.contains(".")) { 061 // Split on the last occurrence of . using a negative lookahead 062 // regexp. 063 String[] parts = name.split("\\.(?=[^\\.]+$)"); 064 name = parts[0]; 065 extension = "." + parts[1]; 066 } 067 NuxeoPrincipal principal = (NuxeoPrincipal) ctx.getPrincipal(); 068 String userName = principal.getName(); // fallback 069 if (!StringUtils.isBlank(principal.getLastName()) && !StringUtils.isBlank(principal.getFirstName())) { 070 // build more user friendly name from user info 071 userName = principal.getFirstName() + " " + principal.getLastName(); 072 } 073 Calendar userDate = Calendar.getInstance(NuxeoDriveManagerImpl.UTC); 074 075 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm"); 076 dateFormat.setCalendar(userDate); 077 String formatedDate = dateFormat.format(userDate.getTime()); 078 String contextSection = String.format(" (%s - %s)", userName, formatedDate); 079 String conflictedName = name + contextSection + extension; 080 return Blobs.createJSONBlobFromValue(conflictedName); 081 } 082 083}