001/* 002 * Copyright 2018 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.runtime.model; 017 018import java.util.HashMap; 019import java.util.List; 020import java.util.Map; 021import java.util.Objects; 022import java.util.Optional; 023 024import org.apache.cocoon.xml.AttributesImpl; 025import org.apache.commons.lang3.StringUtils; 026import org.xml.sax.ContentHandler; 027import org.xml.sax.SAXException; 028 029import org.ametys.core.util.SizeUtils.ExcludeFromSizeCalculation; 030import org.ametys.core.util.XMLUtils; 031 032/** 033 * Simple group of view items 034 */ 035public class SimpleViewItemGroup extends AbstractViewItemGroup 036{ 037 @ExcludeFromSizeCalculation 038 private ViewItemAccessor _parent; 039 040 private String _name; 041 042 public String getName() 043 { 044 return _name; 045 } 046 047 public void setName(String name) 048 { 049 _name = name; 050 } 051 052 public ViewItemAccessor getParent() 053 { 054 return _parent; 055 } 056 057 public void setParent(ViewItemAccessor parent) 058 { 059 _parent = parent; 060 } 061 062 public Map<String, Object> toJSON(DefinitionContext context) 063 { 064 Map<String, Object> result = new HashMap<>(); 065 066 result.put("name", getName()); 067 result.put("label", getLabel()); 068 result.put("description", getDescription()); 069 result.put("role", getRole()); 070 071 List<ViewItem> viewItems = getViewItems(); 072 if (!viewItems.isEmpty()) 073 { 074 result.put("elements", ViewHelper.viewItemsToJSON(viewItems, context)); 075 } 076 077 return result; 078 } 079 080 @SuppressWarnings("static-access") 081 public void toSAX(ContentHandler contentHandler, DefinitionContext context) throws SAXException 082 { 083 AttributesImpl attributes = new AttributesImpl(); 084 Optional.ofNullable(getName()).ifPresent(name -> attributes.addCDATAAttribute("name", name)); 085 Optional.ofNullable(getRole()).ifPresent(role -> attributes.addCDATAAttribute("role", role)); 086 087 XMLUtils.startElement(contentHandler, "group", attributes); 088 089 XMLUtils.createI18nElementIfNotNull(contentHandler, "label", getLabel()); 090 XMLUtils.createI18nElementIfNotNull(contentHandler, "description", getDescription()); 091 092 for (ViewItem viewItem : getViewItems()) 093 { 094 viewItem.toSAX(contentHandler, context); 095 } 096 097 XMLUtils.endElement(contentHandler, "group"); 098 } 099 100 /** 101 * Include the given view to the group. 102 * Add the items of the view to include if they are not already present in the group or the reference view 103 * @param viewToInclude the view to include 104 * @param referenceView the reference view 105 */ 106 public void includeView(View viewToInclude, View referenceView) 107 { 108 ViewHelper.addViewAccessorItems(this, viewToInclude, referenceView, StringUtils.EMPTY); 109 } 110 111 @Override 112 public void copyTo(ViewItem item) 113 { 114 super.copyTo(item); 115 116 assert item instanceof SimpleViewItemGroup; 117 ((SimpleViewItemGroup) item).setName(getName()); 118 } 119 120 public SimpleViewItemGroup createInstance() 121 { 122 return new SimpleViewItemGroup(); 123 } 124 125 @Override 126 public int hashCode() 127 { 128 final int prime = 31; 129 int result = super.hashCode(); 130 result = prime * result + Objects.hash(_name); 131 return result; 132 } 133 134 @Override 135 public boolean equals(Object obj) 136 { 137 if (this == obj) 138 { 139 return true; 140 } 141 if (!super.equals(obj)) 142 { 143 return false; 144 } 145 if (getClass() != obj.getClass()) 146 { 147 return false; 148 } 149 SimpleViewItemGroup other = (SimpleViewItemGroup) obj; 150 return Objects.equals(_name, other._name); 151 } 152 153 @Override 154 public String toString() 155 { 156 return _name + ": " + _children.toString(); 157 } 158}