All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.eclipse.persistence.tools.schemaframework.PopulationManager Maven / Gradle / Ivy
/*
* Copyright (c) 1998, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
// Contributors:
// Oracle - initial API and implementation from Oracle TopLink
package org.eclipse.persistence.tools.schemaframework;
import java.util.*;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.internal.sessions.AbstractSession;
/**
*
* Purpose : This class is used to populate example data into the database, it allows for circular references to be resolved.
*
* Responsibilities :
*
* Allow objects to be registered.
* Allow objects to be looked up.
* Store a globally accessible default instance.
*
*/
public class PopulationManager {
/** Store the objects registered. */
protected Map, Map> registeredObjects;
/** Store the default instance. */
protected static PopulationManager defaultManager;
public PopulationManager() {
registeredObjects = new Hashtable<>();
}
/**
* Add all of the objects of the class and all of its subclasses.
* The session is needed because there is no other way to find all subclasses.
*/
public void addAllObjectsForAbstractClass(Class> objectsClass, AbstractSession session, Vector allObjects) {
ClassDescriptor descriptor = session.getDescriptor(objectsClass);
addAllObjectsForClass(objectsClass, allObjects);
for (ClassDescriptor child : descriptor.getInheritancePolicy().getChildDescriptors()) {
addAllObjectsForAbstractClass(child.getJavaClass(), session, allObjects);
}
}
/**
* Add all of the objects of the class and all of its subclasses.
* The session is needed because there is no other way to find all subclasses.
*/
public void addAllObjectsForAbstractClass(Class> objectsClass, org.eclipse.persistence.sessions.Session session, Vector allObjects) {
addAllObjectsForAbstractClass(objectsClass, (AbstractSession)session, allObjects);
}
/**
* Add all of the objects of the class.
*/
public void addAllObjectsForClass(Class> objectsClass, List allObjects) {
if (!getRegisteredObjects().containsKey(objectsClass)) {
return;
}
for (Object object : getRegisteredObjects().get(objectsClass).values()) {
allObjects.add(object);
}
}
/**
* Check if the object is registered given its name.
*/
public boolean containsObject(Class> objectsClass, String objectsName) {
return ((getRegisteredObjects().containsKey(objectsClass)) && (getRegisteredObjects().get(objectsClass).containsKey(objectsName)));
}
/**
* Check if the object is registered given its name.
*/
public boolean containsObject(Object objectToCheck, String objectsName) {
return containsObject(objectToCheck.getClass(), objectsName);
}
/**
* Return all of the objects registered.
*/
public List> getAllClasses() {
Vector> allClasses = new Vector<>();
allClasses.addAll(getRegisteredObjects().keySet());
return allClasses;
}
/**
* Return all of the objects registered.
*/
public Vector getAllObjects() {
Vector allObjects = new Vector<> ();
for (Class> eachClass : getAllClasses()) {
addAllObjectsForClass(eachClass, allObjects);
}
return allObjects;
}
/**
* Return all of the objects of the class and all of its subclasses.
*/
public List getAllObjectsForAbstractClass(Class> objectsClass) {
List allObjects = new Vector<>();
// hummm, how can this be done....
return allObjects;
}
/**
* Return all of the objects of the class and all of its subclasses.
* The session is needed because there is no other way to find all subclasses.
*/
public List getAllObjectsForAbstractClass(Class> objectsClass, AbstractSession session) {
ClassDescriptor descriptor = session.getDescriptor(objectsClass);
List allObjects = new Vector<>();
addAllObjectsForClass(objectsClass, allObjects);
if (descriptor.hasInheritance()) {
for (ClassDescriptor child : descriptor.getInheritancePolicy().getChildDescriptors()) {
addAllObjectsForClass(child.getJavaClass(), allObjects);
}
}
return allObjects;
}
/**
* Return all of the objects of the class.
*/
public Vector getAllObjectsForClass(Class> objectsClass) {
Vector allObjects = new Vector<>();
addAllObjectsForClass(objectsClass, allObjects);
return allObjects;
}
/**
* Lazy initialize the default instance.
*/
public static PopulationManager getDefaultManager() {
if (defaultManager == null) {
defaultManager = new PopulationManager();
}
return defaultManager;
}
/**
* Return the object registered given its name.
*/
public Object getObject(Class> objectsClass, String objectsName) {
if (!(getRegisteredObjects().containsKey(objectsClass))) {
return null;
}
return getRegisteredObjects().get(objectsClass).get(objectsName);
}
/**
* Return the registered objects.
*/
public Map, Map> getRegisteredObjects() {
return registeredObjects;
}
/**
* Register the object given its name.
* The objects are represented as a hashtable of hashtables, lazy initialized on the class.
*/
public Object registerObject(Class> javaClass, Object objectToRegister, String objectsName) {
if (!(getRegisteredObjects().containsKey(javaClass))) {
getRegisteredObjects().put(javaClass, new Hashtable<>());
}
getRegisteredObjects().get(javaClass).put(objectsName, objectToRegister);
return objectToRegister;
}
/**
* Register the object given its name.
* The objects are represented as a hashtable of hashtables, lazy initialized on the class.
*/
public Object registerObject(Object objectToRegister, String objectsName) {
if (!(getRegisteredObjects().containsKey(objectToRegister.getClass()))) {
getRegisteredObjects().put(objectToRegister.getClass(), new Hashtable<>());
}
getRegisteredObjects().get(objectToRegister.getClass()).put(objectsName, objectToRegister);
return objectToRegister;
}
/**
* Remove the object given its class and name.
*/
public void removeObject(Class> classToRemove, String objectsName) {
if (getRegisteredObjects().containsKey(classToRemove)) {
getRegisteredObjects().get(classToRemove).remove(objectsName);
}
}
/**
* Remove the object given its name.
*/
public Object removeObject(Object objectToRemove, String objectsName) {
removeObject(objectToRemove.getClass(), objectsName);
return objectToRemove;
}
/**
* Reset the default instance.
*/
public static void resetDefaultManager() {
defaultManager = null;
}
/**
* Set the default instance.
*/
public static void setDefaultManager(PopulationManager theDefaultManager) {
defaultManager = theDefaultManager;
}
/**
* Set the registered objects.
*/
public void setRegisteredObjects(Map, Map> registeredObjects) {
this.registeredObjects = registeredObjects;
}
}