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