2.17.2: 2011-06-17

net.sf.basedb.core
Class AnyToAny

java.lang.Object
  extended by net.sf.basedb.core.BasicItem<AnyToAnyData>
      extended by net.sf.basedb.core.AnyToAny
All Implemented Interfaces:
AccessControlled, Identifiable, Nameable

public class AnyToAny
extends BasicItem<AnyToAnyData>
implements Nameable

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: 2010-05-17 12:44:31 +0200 (Mon, 17 May 2010) $

Field Summary
private  BasicItem from
           
private  BasicItem to
           
static Item TYPE
          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
AnyToAny(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
static void deleteStrayLinks()
          Deprecated. Use deleteStrayLinks(ProgressReporter) instead.
static int deleteStrayLinks(ProgressReporter progress)
          Delete all links that are linking to non-existing items.
static boolean exists(DbControl dc, BasicItem from, String name)
          Check if an item has a link with a given name.
static AnyToAny getById(DbControl dc, int id)
          Get an AnyToAny link when you know the id.
static AnyToAny getByName(DbControl dc, BasicItem from, String name)
          Get an AnyToAny link when you know the item it is linking from and the name of the link.
(package private) static AnyToAnyData getDataByName(org.hibernate.Session session, int fromId, Item fromType, String name)
          Get an any-to-any link.
 String getDescription()
          Get the description for the item.
 BasicItem getFrom()
          Get the item this link is linking from.
 int getFromId()
          Get the ID of the item this link is linking from.
 Item getFromType()
          Get the type of the item this link is linking from.
static ItemQuery<AnyToAny> getLinksFrom(BasicItem from)
          Get a query that returns all links linking from the specified item.
static ItemQuery<AnyToAny> getLinksTo(BasicItem to)
          Get a query that returns all links linking to the specified item.
 String getName()
          Get the name of the item.
static AnyToAny getNew(DbControl dc, BasicItem from, BasicItem to, String name, boolean usingTo)
          Create a new AnyToAny link
static AnyToAny getNewOrExisting(DbControl dc, BasicItem from, String name, BasicItem to, boolean usingTo)
          Get a new or existing AnyToAny link.
(package private)  PluginPermission getPluginPermissions()
           
 BasicItem getTo()
          Get the item this link is linking to.
 int getToId()
          Get the ID of the item this link is linking to.
 Item getToType()
          Get the type of the item this link is linking to.
 Item 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 isUsingTo()
          If this link counts as using the destination item.
(package private)  void onBeforeCommit(Transactional.Action action)
          We may have to set the getFromId() and/or getToId() values if those items are also created in the same transaction.
 void setDescription(String description)
          Set the description for the item.
private  void setFrom(BasicItem from)
           
 void setName(String name)
          Set the name of the item.
 void setTo(BasicItem to)
          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
addUsingItems, addUsingItems, checkPermission, equals, getData, getDbControl, getId, getPermissions, getSessionControl, getUsingItems, getVersion, hashCode, hasPermission, isDetached, isInDatabase, onAfterCommit, onAfterInsert, onRollback, setDbControl, setProjectDefaults, toString, toTransferable, validate
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface net.sf.basedb.core.Identifiable
getId, getVersion
 
Methods inherited from interface net.sf.basedb.core.AccessControlled
checkPermission, getPermissions, hasPermission
 

Field Detail

TYPE

public static final Item TYPE
The type of item represented by this class.

See Also:
Item.ANYTOANY, getType()

from

private BasicItem from

to

private BasicItem to
Constructor Detail

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 Detail

getNew

public static AnyToAny getNew(DbControl dc,
                              BasicItem from,
                              BasicItem to,
                              String name,
                              boolean usingTo)
                       throws InvalidDataException
Create a new AnyToAny link

Parameters:
dc - The DbControl which will be used for permission checking and database access
from - The item to create the link from
to - The item to link to
name - The name of the link, must be unique among all links for the from item
usingTo - TRUE if the link counts as using the to item, FALSE otherwise (see BasicItem.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 item
BaseException - If there is an error

getById

public static AnyToAny getById(DbControl dc,
                               int id)
                        throws ItemNotFoundException,
                               PermissionDeniedException,
                               BaseException
Get an AnyToAny link when you know the id.

Parameters:
dc - The DbControl which will be used for permission checking and database access
id - The id of the item to load
Returns:
The AnyToAnyLink item
Throws:
ItemNotFoundException - If an item with the specified id is not found
PermissionDeniedException - If the logged in user doesn't have read permission to the item
BaseException - If there is another error

getByName

public static AnyToAny getByName(DbControl dc,
                                 BasicItem from,
                                 String name)
                          throws InvalidDataException,
                                 ItemNotFoundException,
                                 BaseException
Get an AnyToAny link when you know the item it is linking from and the name of the link.

Parameters:
dc - The DbControl which will be used for permission checking and database access.
from - The item the link is linking from
name - The name of the link
Returns:
The AnyToAnyLink item
Throws:
InvalidDataException - If the from or name parameter is null
ItemNotFoundException - If a link with the specified name is not found
BaseException - If there is another error

getDataByName

static AnyToAnyData getDataByName(org.hibernate.Session session,
                                  int fromId,
                                  Item fromType,
                                  String name)
Get an any-to-any link.

Parameters:
session - An open Hibernate session
fromId - The id of the source item
fromType - The type of the source item
name - 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 existing AnyToAny link. If a link already exists it's destination item will be updated to link to the to parameter. If a new link was created you must call DbControl.saveItem(BasicItem) to save the link in the database. Otherwise, you don't have to do anything.

Parameters:
dc - The DbControl which will be used for permission checking and database access
from - The item to create/get the link from
to - The item to link to
name - The name of the link, must be unique among all links for the from item
usingTo - TRUE if the link counts as using the to item, FALSE otherwise (see BasicItem.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 item
BaseException - If there is an error

getLinksFrom

public static ItemQuery<AnyToAny> getLinksFrom(BasicItem from)
                                        throws InvalidDataException
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

public static ItemQuery<AnyToAny> getLinksTo(BasicItem to)
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

unlinkFrom

public static void unlinkFrom(DbControl dc,
                              BasicItem from,
                              String name)
                       throws PermissionDeniedException,
                              InvalidDataException,
                              BaseException
Parameters:
dc - DbControl to use when accessing the database
from - BasicItem to unlink from.
name - Name of the link.
Throws:
PermissionDeniedException - If the logged in user doesn't have WRITE permission for the item
InvalidDataException - If the from or name parameter is null
BaseException - 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 - The DbControl which will be used for permission checking and database access
from - 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 item
InvalidDataException - If the from parameter is null
BaseException - 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 - The DbControl which will be used for permission checking and database access.
from - The item the link is linking from
name - The name of the link
Returns:
TRUE if the link exists, FALSE otherwise
Throws:
InvalidDataException - If the from or name parameter is null
BaseException - If there is another error
Since:
2.4

deleteStrayLinks

public static void deleteStrayLinks()
Deprecated. Use deleteStrayLinks(ProgressReporter) instead.


deleteStrayLinks

public static int deleteStrayLinks(ProgressReporter progress)
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 the BasicItem.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

getType

public 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 the Item enumeration.

Specified by:
getType in interface Identifiable
Returns:
A value indicating the type of item

isUsed

public boolean isUsed()
               throws BaseException
Always FALSE.

Overrides:
isUsed in class BasicItem<AnyToAnyData>
Returns:
TRUE if this item is used, FALSE otherwise
Throws:
BaseException - If not able to tell if item is used or not.
See Also:
BasicItem.getUsingItems()

onBeforeCommit

void onBeforeCommit(Transactional.Action action)
              throws BaseException
We may have to set the getFromId() and/or getToId() values if those items are also created in the same transaction.

Overrides:
onBeforeCommit in class BasicItem<AnyToAnyData>
Throws:
BaseException - If there is an error
See Also:
Transactional, Core API overview - Transaction handling, Coding rules and guidelines for item classes

initPermissions

void initPermissions(int granted,
                     int denied)
               throws BaseException
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 class BasicItem<AnyToAnyData>
Parameters:
granted - Permissions that have been granted by the subclass
denied - Permissions that have been denied by the subclass
Throws:
BaseException - If the permissions couldn't be initialised

getPluginPermissions

PluginPermission getPluginPermissions()
Overrides:
getPluginPermissions in class BasicItem<AnyToAnyData>

getName

public String getName()
Description copied from interface: Nameable
Get the name of the item.

Specified by:
getName in interface Nameable
Returns:
A String with the name of the item

setName

public void setName(String name)
             throws PermissionDeniedException,
                    InvalidDataException
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 the Nameable.MAX_NAME_LENGTH constant.

Specified by:
setName in interface Nameable
Parameters:
name - The new name for the item
Throws:
PermissionDeniedException - If the logged in user doesn't have write permission
InvalidDataException - If the name is null or longer than specified by the Nameable.MAX_NAME_LENGTH constant

getDescription

public String getDescription()
Description copied from interface: Nameable
Get the description for the item.

Specified by:
getDescription in interface Nameable
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 the Nameable.MAX_DESCRIPTION_LENGTH constant.

Specified by:
setDescription in interface Nameable
Parameters:
description - The new description for the item
Throws:
PermissionDeniedException - If the logged in user doesn't have write permission
InvalidDataException - If the description longer than specified by the Nameable.MAX_DESCRIPTION_LENGTH constant

setFrom

private void setFrom(BasicItem from)
              throws InvalidDataException,
                     PermissionDeniedException
Throws:
InvalidDataException
PermissionDeniedException

getFromId

public int getFromId()
Get the ID of the item this link is linking from.

Returns:
The ID

getFromType

public Item getFromType()
Get the type of the item this link is linking from.

Returns:
The type of item

getFrom

public BasicItem getFrom()
                  throws PermissionDeniedException,
                         BaseException
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 item
BaseException - If there is another error

getToId

public int getToId()
Get the ID of the item this link is linking to.

Returns:
The ID

getToType

public Item getToType()
Get the type of the item this link is linking to.

Returns:
The type of the item

getTo

public BasicItem getTo()
                throws PermissionDeniedException,
                       BaseException
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 item
BaseException - If there is another error

setTo

public void setTo(BasicItem to)
           throws InvalidDataException,
                  PermissionDeniedException
Set the destination item of the link.

Parameters:
to - The destination item
Throws:
InvalidDataException - If the item is null or is an AnyToAny link
PermissionDeniedException - 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

public void setUsingTo(boolean usesTo)
                throws PermissionDeniedException
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

2.17.2: 2011-06-17