001/*
002 * (C) Copyright 2013 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-2.1.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 *     dmetzler
016 */
017package org.nuxeo.ecm.restapi.jaxrs.io.directory;
018
019import java.io.IOException;
020
021import javax.ws.rs.Produces;
022import javax.ws.rs.core.MediaType;
023import javax.ws.rs.ext.Provider;
024
025import org.codehaus.jackson.JsonGenerator;
026import org.nuxeo.ecm.automation.jaxrs.io.EntityWriter;
027import org.nuxeo.ecm.automation.jaxrs.io.documents.JsonDocumentWriter;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.schema.SchemaManager;
030import org.nuxeo.ecm.core.schema.types.Field;
031import org.nuxeo.ecm.core.schema.types.QName;
032import org.nuxeo.ecm.core.schema.types.Schema;
033import org.nuxeo.ecm.directory.Directory;
034import org.nuxeo.ecm.directory.api.DirectoryEntry;
035import org.nuxeo.ecm.directory.api.DirectoryService;
036import org.nuxeo.ecm.webengine.jaxrs.coreiodelegate.JsonCoreIODelegate;
037import org.nuxeo.runtime.api.Framework;
038
039/**
040 * @since 5.7.3
041 * @deprecated since 7.10 The Nuxeo JSON marshalling was migrated to nuxeo-core-io. This class is replaced by
042 *             org.nuxeo.ecm.directory.io.DirectoryEntrysonWriter which is registered by default and available to
043 *             marshal {@link DirectoryEntry} from the Nuxeo Rest API thanks to the JAX-RS marshaller
044 *             {@link JsonCoreIODelegate}.
045 */
046@Deprecated
047@Provider
048@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON + "+nxentity" })
049public class DirectoryEntryWriter extends EntityWriter<DirectoryEntry> {
050
051    /**
052     *
053     */
054    public static final String ENTITY_TYPE = "directoryEntry";
055
056    @Override
057    protected void writeEntityBody(JsonGenerator jg, DirectoryEntry directoryEntry) throws IOException {
058        String directoryName = directoryEntry.getDirectoryName();
059
060        jg.writeStringField("directoryName", directoryName);
061        jg.writeObjectFieldStart("properties");
062        DirectoryService ds = Framework.getLocalService(DirectoryService.class);
063        Directory directory = ds.getDirectory(directoryName);
064
065        SchemaManager sm = Framework.getLocalService(SchemaManager.class);
066        Schema schema = sm.getSchema(directory.getSchema());
067
068        DocumentModel entry = directoryEntry.getDocumentModel();
069
070        for (Field field : schema.getFields()) {
071            QName fieldName = field.getName();
072            String key = fieldName.getLocalName();
073
074            jg.writeFieldName(key);
075            if (key.equals(directory.getPasswordField())) {
076                jg.writeString("");
077            } else {
078                JsonDocumentWriter.writePropertyValue(jg, entry.getProperty(fieldName.getPrefixedName()), "");
079            }
080
081        }
082        jg.writeEndObject();
083
084    }
085
086    @Override
087    protected String getEntityType() {
088        return ENTITY_TYPE;
089    }
090
091}