001/* 002 * Copyright 2020 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.repository.model; 017 018import java.util.function.BiConsumer; 019import java.util.function.Consumer; 020 021import org.ametys.runtime.model.ElementDefinition; 022import org.ametys.runtime.model.ModelItemGroup; 023import org.ametys.runtime.model.ModelViewItemGroup; 024import org.ametys.runtime.model.ViewElement; 025import org.ametys.runtime.model.ViewItem; 026import org.ametys.runtime.model.ViewItemAccessor; 027import org.ametys.runtime.model.ViewItemGroup; 028 029/** 030 * Helper for manipulating views in the context of the repository plugin (aware of repeaters, composites, ...). 031 */ 032public final class ViewHelper 033{ 034 private ViewHelper() 035 { 036 // Empty constructor 037 } 038 039 /** 040 * Visit a view, allowing to perform specific actions for view elements. 041 * @param viewItemAccessor the {@link ViewItemAccessor} to visit. 042 * @param elementConsumer the consumer called on each {@link ViewElement}. 043 * @param compositeConsumer the consumer called on each item refering to a {@link CompositeDefinition}. 044 * @param repeaterConsumer the consumer called on each item refering to a {@link RepeaterDefinition}. 045 * @param groupConsumer the consumer called on each other {@link ViewItemGroup}. 046 */ 047 public static void visitView(ViewItemAccessor viewItemAccessor, BiConsumer<ViewElement, ElementDefinition> elementConsumer, BiConsumer<ModelViewItemGroup, CompositeDefinition> compositeConsumer, BiConsumer<ModelViewItemGroup, RepeaterDefinition> repeaterConsumer, Consumer<ViewItemGroup> groupConsumer) 048 { 049 for (ViewItem viewItem : viewItemAccessor.getViewItems()) 050 { 051 if (viewItem instanceof ViewElement) 052 { 053 ElementDefinition definition = ((ViewElement) viewItem).getDefinition(); 054 elementConsumer.accept((ViewElement) viewItem, definition); 055 } 056 else if (viewItem instanceof ModelViewItemGroup) 057 { 058 ModelItemGroup modelItemGroup = ((ModelViewItemGroup) viewItem).getDefinition(); 059 060 if (modelItemGroup instanceof CompositeDefinition) 061 { 062 compositeConsumer.accept((ModelViewItemGroup) viewItem, (CompositeDefinition) modelItemGroup); 063 } 064 else if (modelItemGroup instanceof RepeaterDefinition) 065 { 066 repeaterConsumer.accept((ModelViewItemGroup) viewItem, (RepeaterDefinition) modelItemGroup); 067 } 068 } 069 else if (viewItem instanceof ViewItemGroup) 070 { 071 groupConsumer.accept((ViewItemGroup) viewItem); 072 } 073 } 074 } 075}