Files
alfresco-community-repo/source/java/org/alfresco/repo/domain/node/ParentAssocsInfo.java
Raluca Munteanu 86dc6f3402 Merged 5.1.1 (5.1.1) to 5.1.N (5.1.2)
125484 slanglois: MNT-16155 Update source headers - remove old Copyrights from Java and JSP dource files


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.1.N/root@125603 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2016-04-20 10:21:07 +00:00

159 lines
5.4 KiB
Java

package org.alfresco.repo.domain.node;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Object to keep hold of a node and its parent associations.
*
* @author David Ward
* @author Derek Hulley
* @since 3.4
*/
public class ParentAssocsInfo implements Serializable
{
private static final long serialVersionUID = -2167221525380802365L;
private static final Log logger = LogFactory.getLog(ParentAssocsInfo.class);
private static Set<Long> warnedDuplicateParents = new HashSet<Long>(3);
private final boolean isRoot;
private final boolean isStoreRoot;
private final Long primaryAssocId;
private final Map<Long, ChildAssocEntity> parentAssocsById;
/**
* Constructor to provide clean initial version of a Node's parent association
*/
ParentAssocsInfo(boolean isRoot, boolean isStoreRoot, ChildAssocEntity parent)
{
this(isRoot, isStoreRoot, Collections.singletonList(parent));
}
/**
* Constructor to provide clean initial version of a Node's parent associations
*/
ParentAssocsInfo(boolean isRoot, boolean isStoreRoot, List<? extends ChildAssocEntity> parents)
{
this.isRoot = isRoot;
this.isStoreRoot = isStoreRoot;
Long primaryAssocId = null;
// Build compact map of child associations
Map<Long, ChildAssocEntity> parentAssocsById = new TreeMap<Long, ChildAssocEntity>();
for (ChildAssocEntity parentAssoc : parents)
{
Long parentAssocId = parentAssoc.getId();
// Populate the results
parentAssocsById.put(parentAssocId, parentAssoc);
// Primary
if (parentAssoc.isPrimary())
{
if (primaryAssocId == null)
{
primaryAssocId = parentAssocId;
}
else
{
// Warn about the duplicates
synchronized (warnedDuplicateParents)
{
Long childNodeId = parentAssoc.getChildNode().getId();
boolean added = warnedDuplicateParents.add(childNodeId);
if (added)
{
logger.warn(
"Multiple primary associations: \n" +
" Node: " + childNodeId + "\n" +
" Associations: " + parents);
}
}
}
}
}
this.primaryAssocId = primaryAssocId;
// Protect the map from accidental modification
this.parentAssocsById = Collections.unmodifiableMap(parentAssocsById);
}
/**
* Private constructor used to copy existing values.
*/
private ParentAssocsInfo(
boolean isRoot,
boolean isStoreRoot,
Map<Long, ChildAssocEntity> parentAssocsById,
Long primaryAssocId)
{
this.isRoot = isRoot;
this.isStoreRoot = isStoreRoot;
this.parentAssocsById = Collections.unmodifiableMap(parentAssocsById);
this.primaryAssocId = primaryAssocId;
}
@Override
public String toString()
{
StringBuilder builder = new StringBuilder();
builder.append("ParentAssocsInfo ")
.append("[isRoot=").append(isRoot)
.append(", isStoreRoot=").append(isStoreRoot)
.append(", parentAssocsById=").append(parentAssocsById)
.append(", primaryAssocId=").append(primaryAssocId)
.append("]");
return builder.toString();
}
public boolean isRoot()
{
return isRoot;
}
public boolean isStoreRoot()
{
return isStoreRoot;
}
public Map<Long, ChildAssocEntity> getParentAssocs()
{
return parentAssocsById;
}
public ChildAssocEntity getPrimaryParentAssoc()
{
return (primaryAssocId != null) ? parentAssocsById.get(primaryAssocId) : null;
}
public ParentAssocsInfo changeIsRoot(boolean isRoot)
{
return new ParentAssocsInfo(isRoot, this.isRoot, parentAssocsById, primaryAssocId);
}
public ParentAssocsInfo changeIsStoreRoot(boolean isStoreRoot)
{
return new ParentAssocsInfo(this.isRoot, isStoreRoot, parentAssocsById, primaryAssocId);
}
public ParentAssocsInfo addAssoc(Long assocId, ChildAssocEntity parentAssoc)
{
Map<Long, ChildAssocEntity> parentAssocs = new HashMap<Long, ChildAssocEntity>(parentAssocsById);
parentAssocs.put(parentAssoc.getId(), parentAssoc);
return new ParentAssocsInfo(isRoot, isStoreRoot, parentAssocs, primaryAssocId);
}
public ParentAssocsInfo removeAssoc(Long assocId)
{
Map<Long, ChildAssocEntity> parentAssocs = new HashMap<Long, ChildAssocEntity>(parentAssocsById);
parentAssocs.remove(assocId);
return new ParentAssocsInfo(isRoot, isStoreRoot, parentAssocs, primaryAssocId);
}
}