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.Map; 020 021import org.apache.cocoon.ProcessingException; 022import org.apache.commons.lang3.StringUtils; 023 024import org.ametys.plugins.core.ui.help.HelpLink; 025 026/** 027 * View reference to an element definition 028 */ 029public class ViewElement implements ModelViewItem<ElementDefinition> 030{ 031 private ElementDefinition _definition; 032 public ElementDefinition getDefinition() 033 { 034 return _definition; 035 } 036 037 public void setDefinition(ElementDefinition definition) 038 { 039 if (definition == null) 040 { 041 throw new IllegalArgumentException("Try to set a null definition to the view element"); 042 } 043 _definition = definition; 044 } 045 046 public String getName() 047 { 048 if (_definition != null) 049 { 050 return _definition.getName(); 051 } 052 else 053 { 054 return null; 055 } 056 } 057 058 public Map<String, Object> toJSON() throws ProcessingException 059 { 060 Map<String, Object> result = new HashMap<>(); 061 062 if (getDefinition() != null) 063 { 064 ElementDefinition definition = getDefinition(); 065 Map<String, Object> json = definition.toJSON(); 066 067 Model model = definition.getModel(); 068 if (model != null) 069 { 070 String modelId = model.getId(); 071 String family = model.getFamilyId(); 072 073 String path = definition.getPath(); 074 //If there is a path, and it does not starts with '/', we add one at the beginning 075 if (StringUtils.isNotBlank(path)) 076 { 077 path = StringUtils.prependIfMissing(path, ModelItem.ITEM_PATH_SEPARATOR); 078 } 079 String featureId = StringUtils.join(modelId, path); 080 //Remove the starting '/' if present 081 featureId = StringUtils.removeStart(featureId, ModelItem.ITEM_PATH_SEPARATOR); 082 083 HelpLink help = new HelpLink(family, featureId, null); 084 json.put("help", help); 085 } 086 result.put(definition.getName(), json); 087 } 088 089 return result; 090 } 091}