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