Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.celements.convert.bean;

import java.util.function.Supplier;

import javax.validation.constraints.NotNull;

import org.xwiki.component.annotation.ComponentRole;
Expand All @@ -9,7 +11,6 @@
import com.celements.common.reflect.ReflectiveInstanceSupplier;
import com.celements.component.ComponentInstanceSupplier;
import com.celements.convert.Converter;
import com.google.common.base.Supplier;

/**
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import com.celements.model.classes.fields.ClassField;
import com.celements.model.classes.fields.list.ListField;
import com.celements.model.field.AbstractFieldAccessor;
import com.celements.model.field.FieldAccessException;
import com.celements.model.field.FieldAccessor;
import com.celements.model.field.FieldMissingException;
Expand All @@ -23,7 +22,7 @@
* {{@link #getBeanMethodName(ClassField)} to check expected naming.
*/
@Component(BeanFieldAccessor.NAME)
public class BeanFieldAccessor<T> extends AbstractFieldAccessor<T> {
public class BeanFieldAccessor<T> implements FieldAccessor<T> {

private static final Logger LOGGER = LoggerFactory.getLogger(BeanFieldAccessor.class);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.celements.convert.bean;

import java.util.function.Supplier;

import org.xwiki.component.annotation.Component;
import org.xwiki.component.annotation.InstantiationStrategy;
import org.xwiki.component.annotation.Requirement;
import org.xwiki.component.descriptor.ComponentInstantiationStrategy;

import com.celements.convert.classes.XObjectDeconverter;
import com.celements.model.field.FieldAccessor;
import com.google.common.base.Supplier;
import com.xpn.xwiki.objects.BaseObject;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.celements.convert.bean;

import static com.google.common.base.Preconditions.*;

import java.util.function.Supplier;

import javax.inject.Inject;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.xwiki.component.annotation.ComponentRole;

import com.celements.common.reflect.ReflectiveInstanceSupplier;
import com.celements.component.ComponentInstanceSupplier;
import com.celements.convert.classes.AbstractObjectConverter;
import com.celements.model.field.CelObjectFieldAccessor;
import com.celements.model.field.FieldAccessor;
import com.xpn.xwiki.doc.CelObject;

/** Converts an immutable {@link CelObject} to a bean. */
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class CelObjectBeanConverter<T> extends AbstractObjectConverter<CelObject, T> implements
BeanClassDefConverter<CelObject, T> {

private final FieldAccessor<CelObject> celObjectAccessor;
private final FieldAccessor<T> beanAccessor;
private Supplier<T> supplier;

@Inject
public CelObjectBeanConverter(
CelObjectFieldAccessor celObjectAccessor,
BeanFieldAccessor<T> beanAccessor) {
this.celObjectAccessor = celObjectAccessor;
this.beanAccessor = beanAccessor;
}

@Override
public void initialize(Supplier<T> instanceSupplier) {
supplier = checkNotNull(instanceSupplier);
}

@Override
public void initialize(Class<T> token) {
checkNotNull(token);
initialize(token.isAnnotationPresent(ComponentRole.class)
? new ComponentInstanceSupplier<>(token)
: new ReflectiveInstanceSupplier<>(token));
}

@Override
public String getName() {
return getClass().getSimpleName();
}

@Override
public FieldAccessor<CelObject> getFromFieldAccessor() {
return celObjectAccessor;
}

@Override
public FieldAccessor<T> getToFieldAccessor() {
return beanAccessor;
}

@Override
protected Supplier<T> getInstanceSupplier() {
checkState(supplier != null, "not initialized");
return supplier;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static com.google.common.base.Preconditions.*;

import java.util.function.Supplier;

import org.xwiki.component.annotation.Component;
import org.xwiki.component.annotation.ComponentRole;
import org.xwiki.component.annotation.InstantiationStrategy;
Expand All @@ -11,9 +13,9 @@
import com.celements.common.reflect.ReflectiveInstanceSupplier;
import com.celements.component.ComponentInstanceSupplier;
import com.celements.convert.classes.ClassDefinitionConverter;
import com.celements.convert.classes.XObjectConverter;
import com.celements.convert.classes.AbstractObjectConverter;
import com.celements.model.field.FieldAccessor;
import com.google.common.base.Supplier;
import com.celements.model.field.XObjectFieldAccessor;
import com.xpn.xwiki.objects.BaseObject;

/**
Expand All @@ -22,13 +24,16 @@
*/
@Component(XObjectBeanConverter.NAME)
@InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP)
public class XObjectBeanConverter<T> extends XObjectConverter<T> implements
public class XObjectBeanConverter<T> extends AbstractObjectConverter<BaseObject, T> implements
BeanClassDefConverter<BaseObject, T> {

public static final String NAME = "xobjectbean";

private Supplier<T> supplier;

@Requirement(XObjectFieldAccessor.NAME)
private FieldAccessor<BaseObject> xObjAccessor;

@Requirement(BeanFieldAccessor.NAME)
private FieldAccessor<T> beanAccessor;

Expand All @@ -52,6 +57,11 @@ public String getName() {
return NAME;
}

@Override
public FieldAccessor<BaseObject> getFromFieldAccessor() {
return xObjAccessor;
}

@Override
public FieldAccessor<T> getToFieldAccessor() {
return beanAccessor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static com.celements.common.MoreObjectsCel.*;
import static com.google.common.base.Preconditions.*;

import java.util.function.Supplier;

import javax.validation.constraints.NotNull;

import org.slf4j.Logger;
Expand All @@ -14,7 +16,6 @@
import com.celements.model.field.FieldAccessException;
import com.celements.model.field.FieldAccessor;
import com.celements.model.field.FieldMissingException;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;

public abstract class AbstractClassDefConverter<A, B> implements ClassDefinitionConverter<A, B> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,14 @@

import com.celements.model.classes.ClassDefinition;
import com.celements.model.classes.fields.ClassField;
import com.celements.model.field.FieldAccessor;
import com.celements.model.field.XObjectFieldAccessor;
import com.celements.web.classes.oldcore.XWikiObjectClass;
import com.google.common.collect.ImmutableList;
import com.xpn.xwiki.objects.BaseObject;

public abstract class XObjectConverter<T> extends AbstractClassDefConverter<BaseObject, T> {
public abstract class AbstractObjectConverter<O, T> extends AbstractClassDefConverter<O, T> {

@Requirement(XWikiObjectClass.CLASS_DEF_HINT)
private ClassDefinition xObjClassDef;

@Requirement(XObjectFieldAccessor.NAME)
private FieldAccessor<BaseObject> xObjAccessor;

@Override
public FieldAccessor<BaseObject> getFromFieldAccessor() {
return xObjAccessor;
}

@Override
protected ImmutableList.Builder<ClassField<?>> aggregateClassFields(
ImmutableList.Builder<ClassField<?>> iter) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.celements.convert.classes;

import java.util.function.Supplier;

import org.xwiki.component.annotation.Requirement;

import com.celements.model.field.FieldAccessor;
import com.celements.model.field.XObjectFieldAccessor;
import com.celements.model.object.xwiki.XWikiObjectSupplier;
import com.google.common.base.Supplier;
import com.xpn.xwiki.objects.BaseObject;

public abstract class XObjectDeconverter<T> extends AbstractClassDefConverter<T, BaseObject> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;

Expand Down Expand Up @@ -152,6 +152,11 @@ public Document getApiDocument(XWikiDocument doc) throws NoAccessRightsException
EAccessLevel.VIEW);
}

@Override
public Optional<CelDocument> getCelDocument(DocumentReference docRef) {
return getCelDocument(docRef, DEFAULT_LANG);
}

@Override
public Optional<CelDocument> getCelDocument(DocumentReference docRef, String lang) {
checkNotNull(docRef);
Expand Down Expand Up @@ -445,28 +450,37 @@ public boolean isTranslation(XWikiDocument doc) {

@Override
public Stream<XWikiDocument> streamParents(XWikiDocument doc) {
return StreamEx.of(new Iterator<XWikiDocument>() {
return streamParents(doc, XWikiDocument::getParentReference, this::getDocumentOpt);
}

@Override
public Stream<CelDocument> streamParents(CelDocument doc) {
return streamParents(doc, CelDocument::getParentReference, this::getCelDocument);
}

private <D> Stream<D> streamParents(D doc,
Function<D, DocumentReference> parentRef,
Function<DocumentReference, Optional<D>> loader) {
return StreamEx.of(new Iterator<D>() {

private XWikiDocument current = doc;
private D current = doc;
private Set<DocumentReference> seen = new HashSet<>();

@Override
public boolean hasNext() {
return (current != null)
&& (current.getParentReference() != null)
&& exists(current.getParentReference());
return Optional.ofNullable(current)
.map(parentRef)
.filter(DefaultModelAccessFacade.this::exists)
.isPresent();
}

@Override
public XWikiDocument next() {
try {
if (seen.add(current.getParentReference())) {
return current = getDocument(current.getParentReference());
} else {
throw new IllegalStateException("cyclic parent referencing: " + seen);
}
} catch (DocumentNotExistsException | NullPointerException exc) {
throw new NoSuchElementException(exc.getClass().getSimpleName() + " " + exc.getMessage());
public D next() {
DocumentReference ref = Optional.ofNullable(current).map(parentRef).orElseThrow();
if (seen.add(ref)) {
return current = loader.apply(ref).orElseThrow();
} else {
throw new IllegalStateException("cyclic parent referencing: " + seen);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ XWikiDocument getDocument(@NotNull DocumentReference docRef, @Nullable String la
@NotNull
Optional<XWikiDocument> getDocumentOpt(@NotNull DocumentReference docRef, @Nullable String lang);

@NotNull
Optional<CelDocument> getCelDocument(@NotNull DocumentReference docRef);

@NotNull
Optional<CelDocument> getCelDocument(@NotNull DocumentReference docRef,
@Nullable String lang);
Expand Down Expand Up @@ -131,6 +134,9 @@ void deleteDocumentWithoutTranslations(@NotNull XWikiDocument doc, boolean totra
@NotNull
Stream<XWikiDocument> streamParents(@NotNull XWikiDocument doc);

@NotNull
Stream<CelDocument> streamParents(@NotNull CelDocument doc);

/**
* @deprecated instead use {@link XWikiObjectFetcher}
* @param docRef
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.celements.model.field;

import static com.celements.web.classes.oldcore.XWikiDocumentClass.*;
import static com.google.common.base.Preconditions.*;
import static com.google.common.base.Strings.*;
import static java.text.MessageFormat.*;

import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.celements.model.classes.fields.ClassField;

public abstract class AbstractDocumentFieldAccessor<D> implements FieldAccessor<D> {

protected final Logger logger = LoggerFactory.getLogger(this.getClass());

@Override
@SuppressWarnings("unchecked")
public final <V> Optional<V> get(D doc, ClassField<V> field) {
checkNotNull(doc);
checkField(field);
Function<D, ?> getter = getters().get(field.getName());
if (getter == null) {
throw new FieldAccessException("undefined field: " + field);
}
Object value = getter.apply(doc);
if (value instanceof String) {
value = emptyToNull(value.toString().trim());
}
logger.info("get: '{}' for '{}' from '{}'", value, field, doc);
return Optional.ofNullable((V) value);
}

private void checkField(ClassField<?> field) {
checkNotNull(field);
if (!CLASS_REF.equals(field.getClassReference())) {
throw new FieldAccessException(format("uneligible for [{0}], it is of class [{1}]",
CLASS_REF, field.getClassReference()));
}
}

protected abstract Map<String, Function<D, ?>> getters();

}

This file was deleted.

Loading