001/* 002 * Copyright 2022 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.cms.model; 017 018import java.util.Collection; 019import java.util.Collections; 020import java.util.Map; 021 022import org.apache.avalon.framework.service.ServiceException; 023import org.xml.sax.ContentHandler; 024import org.xml.sax.SAXException; 025 026import org.ametys.cms.contenttype.ContentType; 027import org.ametys.cms.contenttype.ContentTypeExtensionPoint; 028import org.ametys.cms.contenttype.ContentTypesHelper; 029import org.ametys.cms.data.ContentValue; 030import org.ametys.core.util.XMLUtils; 031import org.ametys.runtime.model.DefaultElementDefinition; 032import org.ametys.runtime.model.DefinitionContext; 033import org.ametys.runtime.model.ModelItem; 034 035/** 036 * Default implementation for element definition of type Content 037 */ 038public class DefaultContentElementDefinition extends DefaultElementDefinition<ContentValue> implements ContentElementDefinition 039{ 040 private ContentTypesHelper _contentTypesHelper; 041 private ContentTypeExtensionPoint _contentTypeExtensionPoint; 042 043 private String _contentTypeId; 044 045 public String getContentTypeId() 046 { 047 return _contentTypeId; 048 } 049 050 public void setContentTypeId(String contentTypeId) 051 { 052 _contentTypeId = contentTypeId; 053 } 054 055 public Collection< ? extends ModelItem> getModelItems() 056 { 057 if (_contentTypeId != null && _getContentTypeExtensionPoint().hasExtension(_contentTypeId)) 058 { 059 ContentType contentType = _getContentTypeExtensionPoint().getExtension(_contentTypeId); 060 return contentType.getModelItems(); 061 } 062 else 063 { 064 return Collections.singleton(_getContentTypesHelper().getTitleAttributeDefinition()); 065 } 066 } 067 068 @Override 069 protected Map<String, Object> _toJSON(DefinitionContext context) 070 { 071 Map<String, Object> result = super._toJSON(context); 072 result.put("contentType", _contentTypeId); 073 result.put("canCreate", _getContentTypesHelper().hasRight(_contentTypeId)); 074 return result; 075 } 076 077 @Override 078 public void toSAX(ContentHandler contentHandler, DefinitionContext context) throws SAXException 079 { 080 super.toSAX(contentHandler, context); 081 XMLUtils.createElementIfNotNull(contentHandler, "contentType", _contentTypeId); 082 } 083 084 /** 085 * Retrieves the {@link ContentTypesHelper} 086 * @return the {@link ContentTypesHelper} 087 */ 088 protected ContentTypesHelper _getContentTypesHelper() 089 { 090 if (_contentTypesHelper == null) 091 { 092 try 093 { 094 _contentTypesHelper = (ContentTypesHelper) __serviceManager.lookup(ContentTypesHelper.ROLE); 095 } 096 catch (ServiceException e) 097 { 098 throw new RuntimeException("Unable to lookup after the content types helper component", e); 099 } 100 } 101 102 return _contentTypesHelper; 103 } 104 105 /** 106 * Retrieves the {@link ContentTypeExtensionPoint} 107 * @return the {@link ContentTypeExtensionPoint} 108 */ 109 protected ContentTypeExtensionPoint _getContentTypeExtensionPoint() 110 { 111 if (_contentTypeExtensionPoint == null) 112 { 113 try 114 { 115 _contentTypeExtensionPoint = (ContentTypeExtensionPoint) __serviceManager.lookup(ContentTypeExtensionPoint.ROLE); 116 } 117 catch (ServiceException e) 118 { 119 throw new RuntimeException("Unable to lookup after the content type extension point", e); 120 } 121 } 122 123 return _contentTypeExtensionPoint; 124 } 125}