org.modeshape.jcr.federation.FederatedDocumentWriter Maven / Gradle / Ivy
/*
* ModeShape (http://www.modeshape.org)
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership. Some portions may be licensed
* to Red Hat, Inc. under one or more contributor license agreements.
* See the AUTHORS.txt file in the distribution for a full listing of
* individual contributors.
*
* ModeShape is free software. Unless otherwise indicated, all code in ModeShape
* is licensed to you under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* ModeShape is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.modeshape.jcr.federation;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.infinispan.schematic.DocumentFactory;
import org.infinispan.schematic.document.Document;
import org.infinispan.schematic.document.EditableArray;
import org.infinispan.schematic.document.EditableDocument;
import org.modeshape.jcr.JcrLexicon;
import org.modeshape.jcr.cache.document.DocumentTranslator;
import org.modeshape.jcr.federation.spi.Connector;
import org.modeshape.jcr.federation.spi.DocumentWriter;
import org.modeshape.jcr.federation.spi.PageKey;
import org.modeshape.jcr.value.Name;
import org.modeshape.jcr.value.Property;
import org.modeshape.jcr.value.basic.BasicMultiValueProperty;
import org.modeshape.jcr.value.basic.BasicSingleValueProperty;
/**
* Default implementation of the {@link DocumentWriter} interface.
*
* @author Horia Chiorean ([email protected])
*/
public class FederatedDocumentWriter implements DocumentWriter {
private final EditableDocument federatedDocument;
private final DocumentTranslator translator;
public FederatedDocumentWriter( DocumentTranslator translator,
Document document ) {
this.federatedDocument = document != null ? DocumentFactory.newDocument(document) : DocumentFactory.newDocument();
this.translator = translator;
}
public FederatedDocumentWriter( DocumentTranslator translator ) {
this(translator, null);
}
@Override
public FederatedDocumentWriter setId( String id ) {
assert id != null;
translator.setKey(federatedDocument, id);
return this;
}
protected final Name nameFrom( String name ) {
return translator.getNameFactory().create(name);
}
@Override
public DocumentWriter setPrimaryType( Name name ) {
return addProperty(JcrLexicon.PRIMARY_TYPE, name);
}
@Override
public DocumentWriter setPrimaryType( String name ) {
return setPrimaryType(nameFrom(name));
}
@Override
public DocumentWriter addMixinType( Name name ) {
return addPropertyValue(JcrLexicon.MIXIN_TYPES, name);
}
@Override
public DocumentWriter addMixinType( String name ) {
return addMixinType(nameFrom(name));
}
@Override
public DocumentWriter addPropertyValue( Name name,
Object value ) {
boolean knownToBeMultiple = false;
translator.addPropertyValues(federatedDocument, name, knownToBeMultiple, Collections.singleton(value), null);
return this;
}
@Override
public DocumentWriter addPropertyValue( String name,
Object value ) {
return addPropertyValue(nameFrom(name), value);
}
@Override
public DocumentWriter addProperty( String name,
Object value ) {
Name nameObj = nameFrom(name);
return addProperty(nameObj, value);
}
@Override
public DocumentWriter addProperty( String name,
Object[] values ) {
if (values == null) return this;
Name nameObj = nameFrom(name);
return addProperty(nameObj, values);
}
@Override
public DocumentWriter addProperty( String name,
Object firstValue,
Object... additionalValues ) {
Name nameObj = nameFrom(name);
return addProperty(nameObj, firstValue, additionalValues);
}
@Override
public DocumentWriter addProperty( Name name,
Object value ) {
if (value == null) {
return this;
}
translator.setProperty(federatedDocument, new BasicSingleValueProperty(name, value), null);
return this;
}
@Override
public DocumentWriter addProperty( Name name,
Object[] values ) {
if (values == null) return this;
int len = values.length;
if (len == 0) return this;
if (len == 1) {
translator.setProperty(federatedDocument, new BasicSingleValueProperty(name, values[0]), null);
} else {
translator.setProperty(federatedDocument, new BasicMultiValueProperty(name, values), null);
}
return this;
}
@Override
public DocumentWriter addProperty( Name name,
Object firstValue,
Object... additionalValues ) {
if (additionalValues.length == 0) {
translator.setProperty(federatedDocument, new BasicSingleValueProperty(name, firstValue), null);
} else {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy