001/*
002 * (C) Copyright 2006-2011 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 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.opencmis.impl.client;
020
021import java.util.Collections;
022import java.util.List;
023
024import org.apache.chemistry.opencmis.client.api.FileableCmisObject;
025import org.apache.chemistry.opencmis.client.api.Folder;
026import org.apache.chemistry.opencmis.client.api.ObjectId;
027import org.apache.chemistry.opencmis.client.api.ObjectType;
028import org.apache.chemistry.opencmis.client.api.OperationContext;
029import org.apache.chemistry.opencmis.client.api.SecondaryType;
030import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
031import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
032import org.apache.chemistry.opencmis.commons.spi.Holder;
033import org.nuxeo.ecm.core.api.CoreSession;
034import org.nuxeo.ecm.core.api.DocumentModel;
035import org.nuxeo.ecm.core.api.IdRef;
036import org.nuxeo.ecm.core.opencmis.impl.server.NuxeoObjectData;
037
038/**
039 * Base abstract fileable live local CMIS Object.
040 */
041public abstract class NuxeoFileableObject extends NuxeoObject implements FileableCmisObject {
042
043    public NuxeoFileableObject(NuxeoSession session, NuxeoObjectData data, ObjectType type,
044            List<SecondaryType> secondaryTypes) {
045        super(session, data, type, secondaryTypes);
046    }
047
048    @Override
049    public List<Folder> getParents(OperationContext context) {
050        // context ignored
051        CoreSession coreSession = data.doc.getCoreSession();
052        DocumentModel parent = coreSession.getParentDocument(new IdRef(getId()));
053        if (parent == null || nuxeoCmisService.isFilteredOut(parent)) {
054            return Collections.emptyList();
055        }
056        Folder folder = (Folder) session.getObject(parent, session.getDefaultContext());
057        return Collections.singletonList(folder);
058    }
059
060    @Override
061    public List<Folder> getParents() {
062        return getParents(null);
063    }
064
065    @Override
066    public List<String> getPaths() {
067        return Collections.singletonList(data.doc.getPathAsString());
068    }
069
070    @Override
071    public void addToFolder(ObjectId folderId, boolean allVersions) {
072        throw new UnsupportedOperationException("Multi-filing not supported");
073    }
074
075    @Override
076    public void removeFromFolder(ObjectId folderId) {
077        service.removeObjectFromFolder(getRepositoryId(), getId(), folderId == null ? null : folderId.getId(), null);
078    }
079
080    @Override
081    public NuxeoFileableObject move(ObjectId sourceFolder, ObjectId targetFolder, OperationContext context) {
082        // context ignored
083        Holder<String> objectIdHolder = new Holder<String>(getId());
084        if (sourceFolder == null) {
085            throw new CmisInvalidArgumentException("Missing source folder");
086        }
087        if (targetFolder == null) {
088            throw new CmisInvalidArgumentException("Missing target folder");
089        }
090        service.moveObject(getRepositoryId(), objectIdHolder, targetFolder.getId(), sourceFolder.getId(), null);
091        return this;
092    }
093
094    @Override
095    public NuxeoFileableObject move(ObjectId sourceFolder, ObjectId targetFolder) {
096        return move(sourceFolder, targetFolder, null);
097    }
098
099}