Package org.ametys.runtime.model
Class ViewHelper
java.lang.Object
org.ametys.runtime.model.ViewHelper
Helper class for views
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
The mode used to insert a view item in an existing view item accessor -
Method Summary
Modifier and TypeMethodDescriptionstatic void
addViewAccessorItems
(ViewItemAccessor currentAccessor, ViewItemAccessor accessorToInclude, ViewItemAccessor referenceViewItemAccessor, String accessorPath) Add the items of the view item accessor to include if they are not already present in the current view item accessor or in the referenced onestatic ModelViewItem
addViewItem
(String relativePath, ViewItemAccessor viewItemAccessor, boolean expandContainers, boolean mergeContainers, ModelItemAccessor... modelItemAccessors) Add a view item in the given accessor.static ModelViewItem
addViewItem
(String relativePath, ViewItemAccessor viewItemAccessor, ModelItemAccessor... modelItemAccessors) Add a view item in the given accessor.static boolean
areItemsPresentsOnlyOnce
(View view) Checks if all the items of the given view are present only once in the view Children of accessors that are not containers are not taken into accountcopyViewItems
(List<ViewItem> viewItems) Copy the given view items.Copy the given view items.static <T extends ViewItemAccessor>
TcreateEmptyViewItemAccessor
(Collection<? extends ModelItemAccessor> modelItemAccessors) Creates an emptyViewItemAccessor
The created container can be aView
, aModelViewItemGroup
or aViewElementAccessor
, according to the givenModelItemAccessor
s.static ModelViewItem
createModelViewItemInstance
(ModelItem modelItem) Creates an instance ofModelViewItem
due to the givenModelItem
static <T extends ViewItemAccessor>
TcreateViewItemAccessor
(Collection<? extends ModelItemAccessor> modelItemAccessors) Creates aViewItemAccessor
with the items of the givenModelItemAccessor
static <T extends ViewItemAccessor>
TcreateViewItemAccessor
(Collection<? extends ModelItemAccessor> modelItemAccessors, String... itemPaths) Creates aViewItemAccessor
with the given itemsstatic ViewItem
createViewItemInstance
(ViewItem viewItem) Create an instance ofViewItem
from the given onestatic Collection<ModelItem>
getModelItems
(ViewItemAccessor viewItemAccessor) Get the collection of model items from a view item accessor.static Collection<ModelItem>
getModelItemsFromView
(View view) Retrieves all model items in the givenView
Retrieves the paths of all model items in the givenView
static ModelViewItem
getModelViewItem
(ViewItemAccessor viewItemAccessor, String itemPath) Gets theModelViewItem
from theViewItemAccessor
at the given path.static ModelViewItemGroup
getModelViewItemGroup
(ViewItemAccessor viewItemAccessor, String itemPath) Gets theModelViewItemGroup
from theViewItemAccessor
at the given path.static String
getModelViewItemPath
(ModelViewItem modelViewItem) Retrieves the path of the givenModelViewItem
.static SimpleViewItemGroup
getSimpleViewItemGroup
(ViewItemAccessor viewItemAccessor, String groupPath) Retrieves theSimpleViewItemGroup
at the given group path.static View
getTruncatedView
(View originalView) Retrieves a View corresponding to the given one, avoiding the view items below theViewItemAccessor
s that are notViewItemContainer
sstatic ViewElement
getViewElement
(ViewItemAccessor viewItemAccessor, String itemPath) Gets theViewElement
from theViewItemAccessor
at the given path.static ViewItem
getViewItem
(ViewItemAccessor viewItemAccessor, String viewItemPath) Retrieves theViewItem
at the given view path.static boolean
hasModelViewItem
(ViewItemAccessor viewItemAccessor, String itemPath) Checks if there is aModelViewItem
in theViewItemAccessor
at the given pathstatic void
insertItemAfterOrBefore
(ViewItemAccessor viewItemAccessor, ViewItem itemToInsert, String insertAfterOrBefore, ViewHelper.InsertMode insertMode) Insert the givenViewItem
to the view before or after the item at the given pathstatic void
insertItemBefore
(ViewItemAccessor viewItemAccessor, ViewItem viewItemToInsert, String insertBefore) Insert the givenViewItem
to the view before the item at the given pathstatic void
insertViewItemAfter
(ViewItemAccessor viewItemAccessor, ViewItem viewItemToInsert, String insertAfter) Insert the givenViewItem
to the view after the item at the given pathstatic <T extends ViewItemAccessor>
TmergeDuplicatedItems
(T originalViewItemAccessor) Retrieves aViewItemAccessor
corresponding to the given one, where items that appears several times are mergedviewItemsToJSON
(List<ViewItem> viewItems, DefinitionContext context) Converts the given view items as a JSON map
-
Method Details
-
createModelViewItemInstance
Creates an instance ofModelViewItem
due to the givenModelItem
- Parameters:
modelItem
- the model item corresponding to the view item to create- Returns:
- the created view item
-
getModelViewItemPath
Retrieves the path of the givenModelViewItem
. Only keep segments of the model view items, does not include groups- Parameters:
modelViewItem
- the model view item- Returns:
- the model view item's path
-
copyViewItems
Copy the given view items. Also copy the children of view item accessors- Parameters:
viewItems
- the view items to copy- Returns:
- the view items copies
-
copyViewItems
public static List<ViewItem> copyViewItems(List<ViewItem> viewItems, Function<ViewItem, ViewItem> createCopyInstance) Copy the given view items. Also copy the children of view item accessors- Parameters:
viewItems
- the view items to copycreateCopyInstance
- the method to use to create the view item instances- Returns:
- the view items copies
-
createViewItemInstance
Create an instance ofViewItem
from the given one -
addViewAccessorItems
public static void addViewAccessorItems(ViewItemAccessor currentAccessor, ViewItemAccessor accessorToInclude, ViewItemAccessor referenceViewItemAccessor, String accessorPath) Add the items of the view item accessor to include if they are not already present in the current view item accessor or in the referenced one- Parameters:
currentAccessor
- the current accessoraccessorToInclude
- the accessor to includereferenceViewItemAccessor
- the reference view item accessoraccessorPath
- the path of the accessor relative to the reference view item accessor
-
createViewItemAccessor
public static <T extends ViewItemAccessor> T createViewItemAccessor(Collection<? extends ModelItemAccessor> modelItemAccessors) throws IllegalArgumentException Creates aViewItemAccessor
with the items of the givenModelItemAccessor
- Type Parameters:
T
- Type of theViewItemAccessor
to create (View
,ModelViewItemGroup
orViewElementAccessor
)- Parameters:
modelItemAccessors
- the model item accessors- Returns:
- the created
ViewItemAccessor
- Throws:
IllegalArgumentException
- if the model item accessors collection is empty
-
createViewItemAccessor
public static <T extends ViewItemAccessor> T createViewItemAccessor(Collection<? extends ModelItemAccessor> modelItemAccessors, String... itemPaths) throws IllegalArgumentException, BadItemTypeException Creates aViewItemAccessor
with the given items- Type Parameters:
T
- Type of theViewItemAccessor
to create (View
,ModelViewItemGroup
orViewElementAccessor
)- Parameters:
modelItemAccessors
- the model items accessing the items definitionsitemPaths
- the paths of the items to put in the view item accessor- Returns:
- the created
ViewItemAccessor
- Throws:
IllegalArgumentException
- if the model item containers collection is empty or if an item path isnull
, empty, or is not defined in the given model item containersBadItemTypeException
- if a segment in a path (but not the last) does not represent a group item
-
createEmptyViewItemAccessor
public static <T extends ViewItemAccessor> T createEmptyViewItemAccessor(Collection<? extends ModelItemAccessor> modelItemAccessors) throws IllegalArgumentException Creates an emptyViewItemAccessor
The created container can be aView
, aModelViewItemGroup
or aViewElementAccessor
, according to the givenModelItemAccessor
s.- Type Parameters:
T
- The type of the created accessor- Parameters:
modelItemAccessors
- the model items accessing the items definitions- Returns:
- the created
ViewItemAccessor
- Throws:
IllegalArgumentException
- if the model item accessors collection is empty
-
addViewItem
public static ModelViewItem addViewItem(String relativePath, ViewItemAccessor viewItemAccessor, ModelItemAccessor... modelItemAccessors) throws IllegalArgumentException, UndefinedItemPathException, BadItemTypeException Add a view item in the given accessor. Only one view item is created for a given container. All paths with the same container are added in the same view item. If the last segment of the given path is aModelItemContainer
, all its children are added to the created view item- Parameters:
relativePath
- path of the item to addviewItemAccessor
- the view item accessormodelItemAccessors
- the corresponding model item accessors- Returns:
- the added view item
- Throws:
IllegalArgumentException
- if the path isnull
or emptyUndefinedItemPathException
- if the path is not defined in the given model item accessorsBadItemTypeException
- if a segment in a path (but not the last) does not represent a group item
-
addViewItem
public static ModelViewItem addViewItem(String relativePath, ViewItemAccessor viewItemAccessor, boolean expandContainers, boolean mergeContainers, ModelItemAccessor... modelItemAccessors) throws IllegalArgumentException, UndefinedItemPathException, BadItemTypeException Add a view item in the given accessor. If mergeContainers is set totrue
, only one view item is created for a given container. All paths with the same container are added in the same view item. If the last segment of the given path is aModelItemContainer
and expandContainers is set totrue
, all its children are added to the created view item- Parameters:
relativePath
- path of the item to addviewItemAccessor
- the view item accessorexpandContainers
- set tofalse
to avoid to create view items for all children of the created view item if it is a containermergeContainers
- set totrue
to avoid to create new containers when they are already present in the given view item accessormodelItemAccessors
- the corresponding model item accessors- Returns:
- the added view item
- Throws:
IllegalArgumentException
- if the path isnull
or emptyUndefinedItemPathException
- if the path is not defined in the given model item accessorsBadItemTypeException
- if a segment in a path (but not the last) does not represent a group item
-
getSimpleViewItemGroup
public static SimpleViewItemGroup getSimpleViewItemGroup(ViewItemAccessor viewItemAccessor, String groupPath) Retrieves theSimpleViewItemGroup
at the given group path. All segments in the group path must refer to aSimpleViewItemGroup
(tab, fieldset) If there are more than one corresponding items, the first one is retrieved- Parameters:
viewItemAccessor
- The ViewItemAccessor in which to find the group of given pathgroupPath
- The group path- Returns:
- the found
SimpleViewItemGroup
- Throws:
IllegalArgumentException
- if the group path is emptyUndefinedItemPathException
- if no group was found at the given path
-
getViewItem
Retrieves theViewItem
at the given view path. If items are in groups, the names of the groups must appear in the path. All groups have to be named If there are more than one corresponding items, the first one is retrieved- Parameters:
viewItemAccessor
- The ViewItemAccessor in which to find the view item of given pathviewItemPath
- The view item path- Returns:
- the found
ViewItem
- Throws:
IllegalArgumentException
- if the view item path is emptyUndefinedItemPathException
- if no view item was found at the given path
-
insertViewItemAfter
public static void insertViewItemAfter(ViewItemAccessor viewItemAccessor, ViewItem viewItemToInsert, String insertAfter) throws IllegalArgumentException, UndefinedItemPathException Insert the givenViewItem
to the view after the item at the given path- Parameters:
viewItemAccessor
- The ViewItemAccessor in which to insert the view itemviewItemToInsert
- The view item to insertinsertAfter
- The name of the view item after which the given one has to be inserted. The item with this name must appear in the given accessor- Throws:
IllegalArgumentException
- If the given view item path is null or emptyUndefinedItemPathException
- If the given view item path is not present in the view item accessor
-
insertItemBefore
public static void insertItemBefore(ViewItemAccessor viewItemAccessor, ViewItem viewItemToInsert, String insertBefore) throws IllegalArgumentException, UndefinedItemPathException Insert the givenViewItem
to the view before the item at the given path- Parameters:
viewItemAccessor
- The ViewItemAccessor in which to insert the itemviewItemToInsert
- The view item to insertinsertBefore
- The name of the view item before which the given one has to be inserted. The item with this name must appear in the given accessor- Throws:
IllegalArgumentException
- If the ModelItem "insertBefore" is null or emptyUndefinedItemPathException
- If the path to the modelItem "insertBefore" is undefined
-
insertItemAfterOrBefore
public static void insertItemAfterOrBefore(ViewItemAccessor viewItemAccessor, ViewItem itemToInsert, String insertAfterOrBefore, ViewHelper.InsertMode insertMode) throws IllegalArgumentException, UndefinedItemPathException Insert the givenViewItem
to the view before or after the item at the given path- Parameters:
viewItemAccessor
- The ViewItemAccessor in which to insert the itemitemToInsert
- The view item to insertinsertAfterOrBefore
- The name of the view item before or after which the given one has to be inserted. The item with this name must appear in the given accessorinsertMode
- The mode of insertion (before or after)- Throws:
IllegalArgumentException
- If the ModelItem "insertAfterOrBefore" is null, empty, or is a pathUndefinedItemPathException
- If the path to the modelItem "insertAfterOrBefore" is undefined
-
getModelItemsPathsFromView
Retrieves the paths of all model items in the givenView
- Parameters:
view
- theView
- Returns:
- the paths of all items
-
getModelItemsFromView
Retrieves all model items in the givenView
- Parameters:
view
- theView
- Returns:
- all model items
-
areItemsPresentsOnlyOnce
Checks if all the items of the given view are present only once in the view Children of accessors that are not containers are not taken into account- Parameters:
view
- the view to check- Returns:
true
if all items are presents only once,false
otherwise
-
getTruncatedView
Retrieves a View corresponding to the given one, avoiding the view items below theViewItemAccessor
s that are notViewItemContainer
s- Parameters:
originalView
- the view to truncate- Returns:
- the truncated view
-
mergeDuplicatedItems
Retrieves aViewItemAccessor
corresponding to the given one, where items that appears several times are merged- Type Parameters:
T
- The type ofViewItemAccessor
to merge- Parameters:
originalViewItemAccessor
- theViewItemAccessor
to merge- Returns:
- the merged
ViewItemAccessor
-
hasModelViewItem
Checks if there is aModelViewItem
in theViewItemAccessor
at the given path- Parameters:
viewItemAccessor
- The accessor of the view itemsitemPath
- The path of the item to check- Returns:
true
if there is a model view item at the given path,false
otherwise
-
viewItemsToJSON
public static Map<String,Object> viewItemsToJSON(List<ViewItem> viewItems, DefinitionContext context) throws ProcessingException Converts the given view items as a JSON map- Parameters:
viewItems
- the view items to convertcontext
- the context of the items' definitions- Returns:
- The view items as a JSON map
- Throws:
ProcessingException
- If an error occurs when converting the view items
-
getModelViewItem
public static ModelViewItem getModelViewItem(ViewItemAccessor viewItemAccessor, String itemPath) throws UndefinedItemPathException, BadItemTypeException Gets theModelViewItem
from theViewItemAccessor
at the given path.
UnlikeViewItemAccessor.getModelViewItem(String)
, this method accepts a path and not only a name, allowing to traverse composites.- Parameters:
viewItemAccessor
- The accessor of view itemsitemPath
- The path of the item to get- Returns:
- The
ModelViewItem
. Can never benull
- Throws:
UndefinedItemPathException
- If one of the parts of the given path is undefinedBadItemTypeException
- If one of the parts of the given path is defined, but is not of the correct type
-
getViewElement
public static ViewElement getViewElement(ViewItemAccessor viewItemAccessor, String itemPath) throws UndefinedItemPathException, BadItemTypeException Gets theViewElement
from theViewItemAccessor
at the given path.
UnlikeViewItemAccessor.getModelViewItem(String)
, this method accepts a path and not only a name, allowing to traverse composites.- Parameters:
viewItemAccessor
- The accessor of view itemsitemPath
- The path of the item to get- Returns:
- The
ViewElement
. Can never benull
- Throws:
UndefinedItemPathException
- If one of the parts of the given path is undefinedBadItemTypeException
- If one of the parts of the given path is defined, but is not of the correct type
-
getModelViewItemGroup
public static ModelViewItemGroup getModelViewItemGroup(ViewItemAccessor viewItemAccessor, String itemPath) throws UndefinedItemPathException, BadItemTypeException Gets theModelViewItemGroup
from theViewItemAccessor
at the given path.
UnlikeViewItemAccessor.getModelViewItem(String)
, this method accepts a path and not only a name, allowing to traverse composites.- Parameters:
viewItemAccessor
- The accessor of view itemsitemPath
- The path of the container to get- Returns:
- The
ModelViewItemGroup
. Can never benull
- Throws:
UndefinedItemPathException
- If one of the parts of the given path is undefinedBadItemTypeException
- If one of the parts of the given path is defined, but is not of the correct type
-
getModelItems
Get the collection of model items from a view item accessor.- Parameters:
viewItemAccessor
- The view item accessor- Returns:
- a collection of model items
-