001/*
002 * (C) Copyright 2011 Nuxeo SA (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 *     matic
016 */
017package org.nuxeo.ecm.platform.ws;
018
019import java.lang.reflect.Field;
020import java.util.List;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.ecm.core.api.CoreSession;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.DocumentRef;
027import org.nuxeo.ecm.platform.api.ws.DocumentLoader;
028import org.nuxeo.ecm.platform.api.ws.DocumentProperty;
029import org.nuxeo.ecm.platform.api.ws.session.WSRemotingSession;
030
031/**
032 * @author matic
033 */
034public class DocumentStateLoader implements DocumentLoader {
035
036    protected static final Log log = LogFactory.getLog(DocumentStateLoader.class);
037
038    @Override
039    public void fillProperties(DocumentModel doc, List<DocumentProperty> props, WSRemotingSession rs)
040            {
041        CoreSession repo = rs.getDocumentManager();
042        DocumentRef ref = doc.getRef();
043        DocumentModel.DocumentModelRefresh dmr = repo.refreshDocument(ref, DocumentModel.REFRESH_STATE, null);
044        for (Field f : dmr.getClass().getDeclaredFields()) {
045            final String fn = f.getName();
046            try {
047                final Object fv = f.get(dmr);
048                if (fv != null) {
049                    DocumentProperty prop = new DocumentProperty(fn, fv.toString());
050                    props.add(prop);
051                }
052            } catch (ReflectiveOperationException e) {
053                log.error("Cannot fetch value for " + ref + ":" + fn, e);
054            }
055        }
056    }
057
058}