Skip to content

Commit

Permalink
Blazebit#414 - Implemented prototype for create views
Browse files Browse the repository at this point in the history
  • Loading branch information
beikov committed May 31, 2017
1 parent 03224c8 commit e314d96
Show file tree
Hide file tree
Showing 78 changed files with 1,346 additions and 693 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@
* limitations under the License.
*/

package com.blazebit.persistence.view.impl.entity;

import javax.persistence.EntityManager;
package com.blazebit.persistence.view;

/**
* The cascade types for updatable entity views.
*
* @author Christian Beikov
* @since 1.2.0
*/
public interface EntityIdAccessor {
public enum CascadeType {

public Object getEntityId(EntityManager em, Object entity);

// TODO: documentation
AUTO,
PERSIST,
UPDATE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,17 @@ public interface EntityViewManager {
* @return The metamodel for this entity view manager
*/
public ViewMetamodel getMetamodel();


/**
* Creates a new instance of the entity view class and returns it.
*
* @param entityViewClass The entity view class to construct
* @param <T></T> The type of the entity view class
* @return A new instance of the given entity view class
* @since 1.2.0
*/
public <T> T create(Class<T> entityViewClass);

/**
* Updates the entity which the given entity view maps to.
* Issues a partial update if enabled for the given view.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.lang.annotation.Target;

/**
* Specifies that the attribute is updatable.
* Specifies the update handling of values assigned to the annotated attribute.
*
* @author Christian Beikov
* @since 1.1.0
Expand All @@ -32,9 +32,30 @@
public @interface UpdatableMapping {

/**
* Specifies whether attribute updates are enabled or not.
*
* Specifies whether the relation for the attribute is updatable.
*
* If the attribute maps a *ToOne relation and {@linkplain #updatable()} is <code>false</code>, the relation isn't updated
* and the referenced entity unless overridden by {@linkplain #cascade()} isn't updated either. If it is set to <code>true</code>, at least the relation updated.
* If the attribute maps a *ToMany relation and {@linkplain #updatable()} is <code>false</code>, changes to the collection
* aren't applied to the collection of the backing entity. If it is set to <code>true</code>, at least the collection changes are applied.
*
* Unless overridden by this attribute, every attribute that also has a setter is updatable.
*
* @return Whether attribute updates are enabled
*/
public boolean updatable() default true;

/**
* The actions that should cascade for the runtime type of objects assigned to the annotated attribute.
* Allows to override the default cascading strategy for a method attribute.
* <p>
* The default strategy {@link CascadeType#AUTO} causes that attributes that are mutable to cascade all changes.
* Updatable and non-updatable attributes that have a non-mutable/immutable type do not cascade changes by default.
* <p>
* An updatable attribute with a non-mutable type can define cascading so that objects of a subtype are persisted or updated.
* A mutable attribute can be excluded from persisting or updating by annotating an empty array of cascade types.
*
* @since 1.2.0
*/
public CascadeType[] cascade() default { CascadeType.AUTO };
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ public interface MethodAttribute<X, Y> extends Attribute<X, Y> {
*/
public boolean isMutable();

/**
* Returns whether the persisting of a referenced object is allowed.
*
* @return Whether persisting should be done
* @since 1.2.0
*/
public boolean isPersistCascaded();

/**
* Returns whether the updating of a referenced object is allowed.
*
* @return Whether updating should be done
* @since 1.2.0
*/
public boolean isUpdateCascaded();

/**
* Returns the getter java method of this attribute.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import com.blazebit.persistence.view.filter.NullFilter;
import com.blazebit.persistence.view.filter.StartsWithFilter;
import com.blazebit.persistence.view.filter.StartsWithIgnoreCaseFilter;
import com.blazebit.persistence.view.impl.entity.DefaultUpdateContext;
import com.blazebit.persistence.view.impl.entity.UpdateContext;
import com.blazebit.persistence.view.impl.filter.ContainsFilterImpl;
import com.blazebit.persistence.view.impl.filter.ContainsIgnoreCaseFilterImpl;
import com.blazebit.persistence.view.impl.filter.EndsWithFilterImpl;
Expand Down Expand Up @@ -166,6 +168,17 @@ public ViewMetamodelImpl getMetamodel() {
return metamodel;
}

@Override
public <T> T create(Class<T> entityViewClass) {
ManagedViewTypeImpl<T> managedViewType = metamodel.managedView(entityViewClass);
Class<? extends T> proxyClass = proxyFactory.getProxy(managedViewType, null);
try {
return proxyClass.getConstructor().newInstance();
} catch (Exception e) {
throw new IllegalArgumentException("Couldn't instantiate entity view object for type: " + entityViewClass.getName(), e);
}
}

@Override
public void update(EntityManager em, Object view) {
update(em, view, true);
Expand All @@ -175,18 +188,34 @@ public void update(EntityManager em, Object view) {
public void updateFull(EntityManager em, Object view) {
update(em, view, false);
}

public void update(EntityManager em, Object view, boolean partial) {
update(new DefaultUpdateContext(this, em), view, partial);
}

private void update(EntityManager em, Object view, boolean partial) {
public void update(UpdateContext context, Object view, boolean partial) {
if (!(view instanceof MutableStateTrackable)) {
throw new IllegalArgumentException("Only updatable entity views can be updated!");
throw new IllegalArgumentException("Can't update non-updatable entity views: " + view);
}

MutableStateTrackable updatableProxy = (MutableStateTrackable) view;
Class<?> entityViewClass = updatableProxy.$$_getEntityViewClass();
ViewType<?> viewType = metamodel.view(entityViewClass);
ManagedViewTypeImpl<?> viewType = metamodel.managedView(entityViewClass);
partial = viewType.isPartiallyUpdatable() && partial;
EntityViewUpdater updater = getUpdater(viewType, partial);
updater.executeUpdate(em, updatableProxy);
updater.executeUpdate(context, updatableProxy);
}

public Object persist(UpdateContext context, Object view) {
if (!(view instanceof MutableStateTrackable)) {
throw new IllegalArgumentException("Can't persist non-updatable entity views: " + view);
}

MutableStateTrackable updatableProxy = (MutableStateTrackable) view;
Class<?> entityViewClass = updatableProxy.$$_getEntityViewClass();
ManagedViewTypeImpl<?> viewType = metamodel.managedView(entityViewClass);
EntityViewUpdater updater = getUpdater(viewType, false);
return updater.executePersist(context, updatableProxy);
}

@Override
Expand Down Expand Up @@ -370,7 +399,7 @@ public ViewTypeObjectBuilderTemplate<?> getTemplate(ExpressionFactory ef, Manage
return value;
}

public EntityViewUpdater getUpdater(ManagedViewType<?> viewType, boolean partial) {
public EntityViewUpdater getUpdater(ManagedViewTypeImpl<?> viewType, boolean partial) {
if (!viewType.isMutable()) {
throw new IllegalArgumentException("Managed view type '" + viewType.getJavaType() + "' is not mutable and can thus not be updated!");
}
Expand All @@ -382,7 +411,7 @@ public EntityViewUpdater getUpdater(ManagedViewType<?> viewType, boolean partial
}
}

private FullEntityViewUpdater getFullUpdater(ManagedViewType<?> viewType) {
private FullEntityViewUpdater getFullUpdater(ManagedViewTypeImpl<?> viewType) {
FullEntityViewUpdater value = fullEntityViewUpdaterCache.get(viewType);

if (value == null) {
Expand All @@ -397,7 +426,7 @@ private FullEntityViewUpdater getFullUpdater(ManagedViewType<?> viewType) {
return value;
}

private PartialEntityViewUpdater getPartialUpdater(ManagedViewType<?> viewType) {
private PartialEntityViewUpdater getPartialUpdater(ManagedViewTypeImpl<?> viewType) {
PartialEntityViewUpdater value = partialEntityViewUpdaterCache.get(viewType);

if (value == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package com.blazebit.persistence.view.impl.collection;

import com.blazebit.persistence.view.impl.entity.UpdateContext;
import com.blazebit.persistence.view.impl.entity.ViewToEntityMapper;

import javax.persistence.EntityManager;
import java.util.Collection;

/**
Expand All @@ -28,6 +28,6 @@
*/
public interface CollectionAction<T extends Collection<?>> {

public void doAction(T collection, EntityManager em, ViewToEntityMapper mapper);
public void doAction(T collection, UpdateContext context, ViewToEntityMapper mapper);

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package com.blazebit.persistence.view.impl.collection;

import com.blazebit.persistence.view.impl.entity.UpdateContext;
import com.blazebit.persistence.view.impl.entity.ViewToEntityMapper;

import javax.persistence.EntityManager;
import java.util.Collection;

/**
Expand All @@ -36,9 +36,9 @@ public CollectionAddAction(E element) {

@Override
@SuppressWarnings("unchecked")
public void doAction(C collection, EntityManager em, ViewToEntityMapper mapper) {
public void doAction(C collection, UpdateContext context, ViewToEntityMapper mapper) {
if (mapper != null) {
collection.add((E) mapper.applyToEntity(em, element));
collection.add((E) mapper.applyToEntity(context, null, element));
} else {
collection.add(element);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package com.blazebit.persistence.view.impl.collection;

import com.blazebit.persistence.view.impl.entity.UpdateContext;
import com.blazebit.persistence.view.impl.entity.ViewToEntityMapper;

import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.Collection;

Expand All @@ -37,13 +37,13 @@ public CollectionAddAllAction(Collection<? extends E> collection) {

@Override
@SuppressWarnings("unchecked")
public void doAction(C collection, EntityManager em, ViewToEntityMapper mapper) {
public void doAction(C collection, UpdateContext context, ViewToEntityMapper mapper) {
if (mapper != null) {
if (collection instanceof ArrayList) {
((ArrayList) collection).ensureCapacity(collection.size() + elements.size());
}
for (Object e : elements) {
collection.add((E) mapper.applyToEntity(em, e));
collection.add((E) mapper.applyToEntity(context, null, e));
}
} else {
collection.addAll(elements);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package com.blazebit.persistence.view.impl.collection;

import com.blazebit.persistence.view.impl.entity.UpdateContext;
import com.blazebit.persistence.view.impl.entity.ViewToEntityMapper;

import javax.persistence.EntityManager;
import java.util.Collection;

/**
Expand All @@ -29,7 +29,7 @@
public class CollectionClearAction<C extends Collection<E>, E> implements CollectionAction<C> {

@Override
public void doAction(C collection, EntityManager em, ViewToEntityMapper mapper) {
public void doAction(C collection, UpdateContext context, ViewToEntityMapper mapper) {
collection.clear();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package com.blazebit.persistence.view.impl.collection;

import com.blazebit.persistence.view.impl.entity.UpdateContext;
import com.blazebit.persistence.view.impl.entity.ViewToEntityMapper;

import javax.persistence.EntityManager;
import java.util.Collection;

/**
Expand All @@ -36,9 +36,9 @@ public CollectionRemoveAction(Object element) {

@Override
@SuppressWarnings("unchecked")
public void doAction(C collection, EntityManager em, ViewToEntityMapper mapper) {
public void doAction(C collection, UpdateContext context, ViewToEntityMapper mapper) {
if (mapper != null) {
collection.remove(mapper.applyToEntity(em, element));
collection.remove(mapper.applyToEntity(context, null, element));
} else {
collection.remove(element);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package com.blazebit.persistence.view.impl.collection;

import com.blazebit.persistence.view.impl.entity.UpdateContext;
import com.blazebit.persistence.view.impl.entity.ViewToEntityMapper;

import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.Collection;

Expand All @@ -37,10 +37,10 @@ public CollectionRemoveAllAction(Collection<?> collection) {

@Override
@SuppressWarnings("unchecked")
public void doAction(C collection, EntityManager em, ViewToEntityMapper mapper) {
public void doAction(C collection, UpdateContext context, ViewToEntityMapper mapper) {
if (mapper != null) {
for (Object e : elements) {
collection.remove(mapper.applyToEntity(em, e));
collection.remove(mapper.applyToEntity(context, null, e));
}
} else {
collection.removeAll(elements);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package com.blazebit.persistence.view.impl.collection;

import com.blazebit.persistence.view.impl.entity.UpdateContext;
import com.blazebit.persistence.view.impl.entity.ViewToEntityMapper;

import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand All @@ -37,11 +37,11 @@ public CollectionRetainAllAction(Collection<?> collection) {
}

@Override
public void doAction(C collection, EntityManager em, ViewToEntityMapper mapper) {
public void doAction(C collection, UpdateContext context, ViewToEntityMapper mapper) {
if (mapper != null) {
List<Object> mappedElements = new ArrayList<>(elements.size());
for (Object e : elements) {
mappedElements.add(mapper.applyToEntity(em, e));
mappedElements.add(mapper.applyToEntity(context, null, e));
}
collection.retainAll(mappedElements);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package com.blazebit.persistence.view.impl.collection;

import com.blazebit.persistence.view.impl.entity.UpdateContext;
import com.blazebit.persistence.view.impl.entity.ViewToEntityMapper;

import javax.persistence.EntityManager;
import java.util.List;

/**
Expand All @@ -29,6 +29,6 @@
public interface ListAction<T extends List<?>> extends CollectionAction<T> {

@Override
public void doAction(T list, EntityManager em, ViewToEntityMapper mapper);
public void doAction(T list, UpdateContext context, ViewToEntityMapper mapper);

}
Loading

0 comments on commit e314d96

Please sign in to comment.