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