Package net.sf.basedb.core
Class AnyToAny
java.lang.Object
net.sf.basedb.core.BasicItem
net.sf.basedb.core.AnyToAny
- All Implemented Interfaces:
AccessControlled
,Controlled
,Identifiable
,Nameable
,Transactional
An any-to-any link is a named, directed link from one item to another item.
The items can be of any type. Permission-wise the links belong to the start
item. Ie. to be able create, update and delete links a user must have
WRITE permission on the start item. The user must also have USE permission
on the item we are linking to.
All any-to-any links from an item has a name which must be unique for that item.
- Version:
- 2.0
- Author:
- nicklas
- Last modified
- $Date: 2024-01-08 15:38:53 +0100 (Mon, 08 Jan 2024) $
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
AnyToAny.FastLinkLoader<S extends BasicItem,
T extends BasicItem> Helper class for faster loading of any-to-any links between items.Nested classes/interfaces inherited from interface net.sf.basedb.core.Transactional
Transactional.Action
-
Field Summary
Modifier and TypeFieldDescriptionprivate BasicItem
private BasicItem
static final Item
The type of item represented by this class.Fields inherited from interface net.sf.basedb.core.Nameable
MAX_DESCRIPTION_LENGTH, MAX_NAME_LENGTH
-
Constructor Summary
ConstructorDescriptionAnyToAny
(AnyToAnyData data) Creates a new link item from the given data.AnyToAny
(AnyToAnyData data, BasicItem from) Special constructor when the from item is known. -
Method Summary
Modifier and TypeMethodDescriptionstatic int
deleteStrayLinks
(ProgressReporter progress) Delete all links that are linking to non-existing items.static boolean
Check if an item has a link with a given name.static AnyToAny
Get anAnyToAny
link when you know the id.static AnyToAny
Get anAnyToAny
link when you know the item it is linking from and the name of the link.(package private) AnyToAnyData
getData()
Get theBasicData
object that holds all data for this item.(package private) static AnyToAnyData
getDataByName
(Session session, int fromId, Item fromType, String name) Get an any-to-any link.Get the description for the item.static List<AnyToAnyLinkStatistics>
getExistingLinkStatistics
(DbControl dc, ItemQuery<?> query, AnyToAnyLinkStatistics.Options options) Get information about existing links from the items that are returned by the given query.static <S extends BasicItem,
T extends BasicItem>
AnyToAny.FastLinkLoader<S,T> getFastLoader
(DbControl dc, Item fromType, String linkName, Item toType) Create an optimized implementation for loading the target item of a given named link from multiple source items.getFrom()
Get the item this link is linking from.int
Get the ID of the item this link is linking from.Get the type of the item this link is linking from.getLinksFrom
(BasicItem from) Get a query that returns all links linking from the specified item.getLinksTo
(BasicItem to) Get a query that returns all links linking to the specified item.getName()
Get the name of the item.static AnyToAny
Create a newAnyToAny
linkstatic AnyToAny
getNewOrExisting
(DbControl dc, BasicItem from, String name, BasicItem to, boolean usingTo) Get a new or existingAnyToAny
link.(package private) PluginPermission
getTo()
Get the item this link is linking to.int
getToId()
Get the ID of the item this link is linking to.Get the type of the item this link is linking to.getType()
Get the type of item represented by the object.(package private) void
initPermissions
(int granted, int denied) Permissions are granted based on the permission on the from item.boolean
isUsed()
Always FALSE.boolean
If this link counts as using the destination item.(package private) void
onBeforeCommit
(Transactional.Action action) We may have to set thegetFromId()
and/orgetToId()
values if those items are also created in the same transaction.void
setDescription
(String description) Set the description for the item.private void
void
Set the name of the item.void
Set the destination item of the link.void
setUsingTo
(boolean usesTo) If this link counts as using the destination item.static int
unlinkAllFrom
(DbControl dc, BasicItem from) Delete all links linking from the specified item.static void
unlinkFrom
(DbControl dc, BasicItem from, String name) Methods inherited from class net.sf.basedb.core.BasicItem
addAnnotatableParents, addUsingItems, addUsingItems, breakCircularReferences, checkBatchAnnotatableUsage, checkPermission, equals, getBasicData, getDbControl, getId, getPermissions, getSessionControl, getUsingItems, getVersion, hashCode, hasPermission, isDetached, isInDatabase, onAfterCommit, onAfterInsert, onRollback, setDbControl, setProjectDefaults, toString, validate
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface net.sf.basedb.core.AccessControlled
checkPermission, getPermissions, hasPermission
Methods inherited from interface net.sf.basedb.core.Identifiable
getId, getVersion
-
Field Details
-
TYPE
The type of item represented by this class.- See Also:
-
from
-
to
-
-
Constructor Details
-
AnyToAny
AnyToAny(AnyToAnyData data) Creates a new link item from the given data. -
AnyToAny
AnyToAny(AnyToAnyData data, BasicItem from) Special constructor when the from item is known.
-
-
Method Details
-
getNew
public static AnyToAny getNew(DbControl dc, BasicItem from, BasicItem to, String name, boolean usingTo) throws InvalidDataException Create a newAnyToAny
link- Parameters:
dc
- TheDbControl
which will be used for permission checking and database accessfrom
- The item to create the link fromto
- The item to link toname
- The name of the link, must be unique among all links for the from itemusingTo
- TRUE if the link counts as using theto
item, FALSE otherwise (seeBasicItem.isUsed()
)- Returns:
- The new
AnyToAny
link - Throws:
InvalidDataException
- If any of the items are null, or if the items are equal or if any of the items is an AnyToAny itemBaseException
- If there is an error
-
getById
public static AnyToAny getById(DbControl dc, int id) throws ItemNotFoundException, PermissionDeniedException, BaseException Get anAnyToAny
link when you know the id.- Parameters:
dc
- TheDbControl
which will be used for permission checking and database accessid
- The id of the item to load- Returns:
- The
AnyToAnyLink
item - Throws:
ItemNotFoundException
- If an item with the specified id is not foundPermissionDeniedException
- If the logged in user doesn't have read permission to the itemBaseException
- If there is another error
-
getByName
public static AnyToAny getByName(DbControl dc, BasicItem from, String name) throws InvalidDataException, ItemNotFoundException, BaseException Get anAnyToAny
link when you know the item it is linking from and the name of the link.- Parameters:
dc
- TheDbControl
which will be used for permission checking and database access.from
- The item the link is linking fromname
- The name of the link- Returns:
- The
AnyToAnyLink
item - Throws:
InvalidDataException
- If the from or name parameter is nullItemNotFoundException
- If a link with the specified name is not foundBaseException
- If there is another error
-
getDataByName
Get an any-to-any link.- Parameters:
session
- An open Hibernate sessionfromId
- The id of the source itemfromType
- The type of the source itemname
- The name of the link- Returns:
- The linked item, or null if no link is found
- Since:
- 2.9
-
getNewOrExisting
public static AnyToAny getNewOrExisting(DbControl dc, BasicItem from, String name, BasicItem to, boolean usingTo) Get a new or existingAnyToAny
link. If a link already exists it's destination item will be updated to link to theto
parameter. If a new link was created you must callDbControl.saveItem(BasicItem)
to save the link in the database. Otherwise, you don't have to do anything.- Parameters:
dc
- TheDbControl
which will be used for permission checking and database accessfrom
- The item to create/get the link fromto
- The item to link toname
- The name of the link, must be unique among all links for the from itemusingTo
- TRUE if the link counts as using theto
item, FALSE otherwise (seeBasicItem.isUsed()
)- Returns:
- The new or existing
AnyToAny
link - Throws:
InvalidDataException
- If any of the items are null, or if the items are equal or if any of the items is an AnyToAny itemBaseException
- If there is an error
-
getLinksFrom
Get a query that returns all links linking from the specified item.- Parameters:
from
- The source of the links- Returns:
- An
ItemQuery
object - Throws:
InvalidDataException
- If the from parameter is null
-
getLinksTo
Get a query that returns all links linking to the specified item.- Parameters:
to
- The destination of the links- Returns:
- An
ItemQuery
object - Throws:
InvalidDataException
- If the to parameter is null
-
getFastLoader
public static <S extends BasicItem,T extends BasicItem> AnyToAny.FastLinkLoader<S,T> getFastLoader(DbControl dc, Item fromType, String linkName, Item toType) Create an optimized implementation for loading the target item of a given named link from multiple source items. When the source item type, target item type and link name is known this implementation can be used to load the target items much more quickly than when loaded one by one. TheAnyToAny.FastLinkLoader.getTo(BasicItem)
is equivalent togetByName(DbControl, BasicItem, String)
.getTo()
.- Since:
- 3.19.8
-
unlinkFrom
public static void unlinkFrom(DbControl dc, BasicItem from, String name) throws PermissionDeniedException, InvalidDataException, BaseException - Parameters:
dc
- DbControl to use when accessing the databasefrom
- BasicItem to unlink from.name
- Name of the link.- Throws:
PermissionDeniedException
- If the logged in user doesn't have WRITE permission for the itemInvalidDataException
- If the from or name parameter is nullBaseException
- If there is another error
-
unlinkAllFrom
public static int unlinkAllFrom(DbControl dc, BasicItem from) throws PermissionDeniedException, InvalidDataException, BaseException Delete all links linking from the specified item.- Parameters:
dc
- TheDbControl
which will be used for permission checking and database accessfrom
- The source of the links- Returns:
- The number of deleted links
- Throws:
PermissionDeniedException
- If the logged in user doesn't have WRITE permission for the itemInvalidDataException
- If the from parameter is nullBaseException
- If there is another error
-
exists
public static boolean exists(DbControl dc, BasicItem from, String name) throws InvalidDataException, BaseException Check if an item has a link with a given name.- Parameters:
dc
- TheDbControl
which will be used for permission checking and database access.from
- The item the link is linking fromname
- The name of the link- Returns:
- TRUE if the link exists, FALSE otherwise
- Throws:
InvalidDataException
- If the from or name parameter is nullBaseException
- If there is another error- Since:
- 2.4
-
getExistingLinkStatistics
public static List<AnyToAnyLinkStatistics> getExistingLinkStatistics(DbControl dc, ItemQuery<?> query, AnyToAnyLinkStatistics.Options options) Get information about existing links from the items that are returned by the given query. If the query returns a lot of items a max number of samples to be used may be specified. The samples are randomly selected. SeeAnyToAnyLinkStatistics.Options
for more information.- Returns:
- A list with statistics about links (can be empty)
- Since:
- 3.17
-
deleteStrayLinks
Delete all links that are linking to non-existing items. This method is intended to be executed at regular intervals by a cleanup application. Although theBasicItem.onBeforeCommit(Transactional.Action)
method tries to delete as many links as it can, it will not not delete links leading to items that have been deleted by Hibernate cascade.- Parameters:
progress
- An optional progress reporter- Since:
- 2.13
-
getData
AnyToAnyData getData()Description copied from class:BasicItem
Get theBasicData
object that holds all data for this item. -
getType
Description copied from interface:Identifiable
Get the type of item represented by the object. The returned value is one of the values defined in theItem
enumeration.- Specified by:
getType
in interfaceIdentifiable
- Returns:
- A value indicating the type of item
-
isUsed
Always FALSE.- Overrides:
isUsed
in classBasicItem
- Returns:
- TRUE if this item is used, FALSE otherwise
- Throws:
BaseException
- If not able to tell if item is used or not.- See Also:
-
onBeforeCommit
We may have to set thegetFromId()
and/orgetToId()
values if those items are also created in the same transaction.- Overrides:
onBeforeCommit
in classBasicItem
- Throws:
BaseException
- If there is an error- See Also:
-
initPermissions
Permissions are granted based on the permission on the from item. READ permission gives READ permission. WRITE permission gives CREATE, WRITE and DELETE permissions.- Overrides:
initPermissions
in classBasicItem
- Parameters:
granted
- Permissions that have been granted by the subclassdenied
- Permissions that have been denied by the subclass- Throws:
BaseException
- If the permissions couldn't be initialised
-
getPluginPermissions
PluginPermission getPluginPermissions()- Overrides:
getPluginPermissions
in classBasicItem
-
getName
Description copied from interface:Nameable
Get the name of the item. -
setName
Description copied from interface:Nameable
Set the name of the item. The name cannot be null and mustn't be longer than the value specified by theNameable.MAX_NAME_LENGTH
constant.- Specified by:
setName
in interfaceNameable
- Parameters:
name
- The new name for the item- Throws:
PermissionDeniedException
- If the logged in user doesn't have write permissionInvalidDataException
- If the name is null or longer than specified by theNameable.MAX_NAME_LENGTH
constant
-
getDescription
Description copied from interface:Nameable
Get the description for the item.- Specified by:
getDescription
in interfaceNameable
- Returns:
- A
String
with a description of the item
-
setDescription
public void setDescription(String description) throws PermissionDeniedException, InvalidDataException Description copied from interface:Nameable
Set the description for the item. The description can be null but mustn't be longer than the value specified by theNameable.MAX_DESCRIPTION_LENGTH
constant.- Specified by:
setDescription
in interfaceNameable
- Parameters:
description
- The new description for the item- Throws:
PermissionDeniedException
- If the logged in user doesn't have write permissionInvalidDataException
- If the description longer than specified by theNameable.MAX_DESCRIPTION_LENGTH
constant
-
setFrom
-
getFromId
public int getFromId()Get the ID of the item this link is linking from.- Returns:
- The ID
-
getFromType
Get the type of the item this link is linking from.- Returns:
- The type of item
-
getFrom
Get the item this link is linking from.- Returns:
- The
BasicItem
item - Throws:
PermissionDeniedException
- If the logged in user doesn't have read permission to the itemBaseException
- If there is another error
-
getToId
public int getToId()Get the ID of the item this link is linking to.- Returns:
- The ID
-
getToType
Get the type of the item this link is linking to.- Returns:
- The type of the item
-
getTo
Get the item this link is linking to.- Returns:
- The
BasicItem
item - Throws:
PermissionDeniedException
- If the logged in user doesn't have read permission to the itemBaseException
- If there is another error
-
setTo
Set the destination item of the link.- Parameters:
to
- The destination item- Throws:
InvalidDataException
- If the item is null or is anAnyToAny
linkPermissionDeniedException
- If the logged in user doesn't have write permission for the link and use permission for the item
-
isUsingTo
public boolean isUsingTo()If this link counts as using the destination item. This affects the possibility to delete items.- Returns:
- TRUE when it is not possible to delete, FALSE otherwise.
-
setUsingTo
If this link counts as using the destination item. This affects the possibility to delete items.- Parameters:
usesTo
- TRUE if this counts as using the item, FALSE otherwise.- Throws:
PermissionDeniedException
- If the logged in user doesn't have write permission
-