001/*
002 * (C) Copyright 2006-2010 Nuxeo SAS (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 *     bstefanescu
016 */
017package org.nuxeo.shell.automation.cmds;
018
019import org.nuxeo.ecm.automation.client.model.DocRef;
020import org.nuxeo.ecm.automation.client.model.PropertyMap;
021import org.nuxeo.shell.Argument;
022import org.nuxeo.shell.Command;
023import org.nuxeo.shell.Context;
024import org.nuxeo.shell.Parameter;
025import org.nuxeo.shell.ShellException;
026import org.nuxeo.shell.automation.DocRefCompletor;
027import org.nuxeo.shell.automation.RemoteContext;
028import org.nuxeo.shell.utils.StringUtils;
029
030/**
031 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
032 */
033@Command(name = "update", help = "Update document properties")
034public class Update implements Runnable {
035
036    @Context
037    protected RemoteContext ctx;
038
039    @Parameter(name = "-s", hasValue = true, help = "Use this to change the separator used in properties. The default is ','")
040    protected String sep = ",";
041
042    @Argument(name = "properties", index = 0, help = "The propertis to update.")
043    protected String props;
044
045    @Argument(name = "path", index = 1, required = false, completor = DocRefCompletor.class, help = "The document path")
046    protected String path;
047
048    public void run() {
049        DocRef doc = ctx.resolveRef(path);
050        try {
051            PropertyMap map = new PropertyMap();
052            for (String pair : props.split(sep)) {
053                String[] ar = StringUtils.split(pair, '=', true);
054                map.set(ar[0], ar[1]);
055            }
056            ctx.getDocumentService().update(doc, map);
057        } catch (Exception e) {
058            throw new ShellException(e);
059        }
060    }
061}