com.gs.fw.common.mithra.util.serializer.SerializationNode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of reladomo Show documentation
Show all versions of reladomo Show documentation
Reladomo is an object-relational mapping framework.
/*
Copyright 2016 Goldman Sachs.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package com.gs.fw.common.mithra.util.serializer;
import com.gs.collections.impl.list.mutable.FastList;
import com.gs.collections.impl.set.mutable.UnifiedSet;
import com.gs.fw.common.mithra.DeepFetchTree;
import com.gs.fw.common.mithra.MithraList;
import com.gs.fw.common.mithra.attribute.AsOfAttribute;
import com.gs.fw.common.mithra.attribute.Attribute;
import com.gs.fw.common.mithra.attribute.MappedAttribute;
import com.gs.fw.common.mithra.finder.AbstractRelatedFinder;
import com.gs.fw.common.mithra.finder.DeepRelationshipAttribute;
import com.gs.fw.common.mithra.finder.Mapper;
import com.gs.fw.common.mithra.finder.RelatedFinder;
import com.gs.fw.common.mithra.util.ListFactory;
import com.gs.reladomo.metadata.ReladomoClassMetaData;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
public class SerializationNode
{
private final SerializationNode parent; // null at root
private List children = ListFactory.EMPTY_LIST;
private List links = ListFactory.EMPTY_LIST;
private List attributes;
private final AbstractRelatedFinder relatedFinder;
private final boolean isLink;
private Attribute[] linkAttributes;
private Class relatedClass;
protected SerializationNode(RelatedFinder relatedFinder)
{
this(null, relatedFinder, false);
}
protected SerializationNode(SerializationNode parent, RelatedFinder relatedFinder)
{
this(parent, relatedFinder, false);
}
protected SerializationNode(SerializationNode parent, RelatedFinder relatedFinder, boolean isLink)
{
this.parent = parent;
this.relatedFinder = ((AbstractRelatedFinder) relatedFinder).zWithoutParentSelector();
this.isLink = isLink;
if (this.isLink)
{
populateLinkAttributes();
}
}
private void populateLinkAttributes()
{
Set allLeftAttributes = this.relatedFinder.zGetMapper().getAllLeftAttributes();
this.linkAttributes = new Attribute[allLeftAttributes.size()];
allLeftAttributes.toArray(this.linkAttributes);
Arrays.sort(this.linkAttributes, new Comparator()
{
@Override
public int compare(Attribute o1, Attribute o2)
{
return o1.getAttributeName().compareTo(o2.getAttributeName());
}
});
}
public static SerializationNode withDefaultAttributes(RelatedFinder finder)
{
SerializationNode result = new SerializationNode(finder);
fillDefaultAttributes(finder, result);
return result;
}
public static SerializationNode withDefaultAttributesAndDeepFetchesFromList(RelatedFinder finder, MithraList list)
{
SerializationNode result = withDefaultAttributes(finder);
DeepFetchTree deepFetchTree = list.getDeepFetchTree();
List children = deepFetchTree.getChildren();
List fullList = FastList.newList();
for(int i=0;i attributes = unwrapAttributes(finder.getPersistentAttributes());
AsOfAttribute[] asOfAttributes = finder.getAsOfAttributes();
if (asOfAttributes != null)
{
for(AsOfAttribute a: asOfAttributes)
{
attributes.add(unwrapAttribute(a));
}
}
result.attributes = attributes;
}
private static FastList unwrapAttributes(Attribute[] persistentAttributes)
{
FastList list = FastList.newList(persistentAttributes.length);
for(Attribute a: persistentAttributes)
{
list.add(unwrapAttribute(a));
}
return list;
}
private static Attribute unwrapAttribute(Attribute a)
{
while (a instanceof MappedAttribute)
{
a = ((MappedAttribute)a).getWrappedAttribute();
}
return a;
}
protected static SerializationNode withDefaultAttributes(SerializationNode parent, RelatedFinder finder)
{
SerializationNode result = new SerializationNode(parent, finder);
fillDefaultAttributes(finder, result);
return result;
}
public List getAttributes()
{
return attributes;
}
public List getChildren()
{
return children;
}
public AbstractRelatedFinder getRelatedFinder()
{
return relatedFinder;
}
public SerializationNode getParent()
{
return parent;
}
public List getLinks()
{
return links;
}
public Attribute[] getLinkAttributes()
{
return linkAttributes;
}
public SerializationNode withoutTheseAttributes(Attribute... attributes)
{
SerializationNode result = new SerializationNode(this.relatedFinder);
result.attributes = this.attributes;
result.children = this.children;
result.links = this.links;
for(Attribute a: attributes)
{
if (a instanceof MappedAttribute)
{
MappedAttribute mappedAttribute = (MappedAttribute) a;
if (result.children == this.children)
{
result.children = FastList.newList(this.children);
}
removeDeepAttributeFromChildren(result, mappedAttribute);
}
else
{
if (result.attributes == this.attributes)
{
result.attributes = FastList.newList(this.attributes);
}
result.attributes.remove(a);
}
}
return result;
}
private void removeDeepAttributeFromChildren(SerializationNode result, MappedAttribute mappedAttribute)
{
for(int i=0;i fullList = new FastList(parentAttribute == null ? 1 : 6);
fullList.add(relatedFinder);
while(parentAttribute != null)
{
fullList.add((AbstractRelatedFinder) parentAttribute);
parentAttribute = parentAttribute.getParentDeepRelationshipAttribute();
}
result.addChildren(fullList);
}
if (!this.links.isEmpty())
{
result = result.withLinks();
}
return result;
}
private boolean addChildren(List fullList)
{
int end = fullList.size() - 1;
SerializationNode cur = this;
boolean added = false;
while(end >= 0)
{
AbstractRelatedFinder toAdd = fullList.get(end);
SerializationNode found = null;
if (cur.children != null)
{
for(int i=0;i fullList, DeepFetchTree deepFetchTree)
{
fullList.add((AbstractRelatedFinder) deepFetchTree.getRelationshipAttribute());
List children = deepFetchTree.getChildren();
for(int i=0;i navigatedSet = UnifiedSet.newSet();
for(int i=0;i allRelationships = this.relatedFinder.getRelationshipFinders();
List newLinks = FastList.newList();
for(int i=0;i newChildren = FastList.newList(this.children.size());
for(int i=0;i dependentRelationshipFinders = this.relatedFinder.getDependentRelationshipFinders();
List fullList = FastList.newList(dependentRelationshipFinders.size());
for(int i=0;i= 0)
{
result.children.set(childIndex, result.children.get(childIndex).withDeepDependents());
}
else
{
SerializationNode child = SerializationNode.withDefaultAttributes(result, relatedFinder);
child = child.withDeepDependents(result);
fullList.add(child);
}
}
result.children.addAll(fullList);
if (!this.links.isEmpty())
{
result = result.withLinks();
}
return result;
}
private int childIndex(List children, RelatedFinder relatedFinder)
{
for(int i=0;i