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.web.repository.page;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.logger.AbstractLogEnabled;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.avalon.framework.service.Serviceable;
027import org.apache.commons.lang3.StringUtils;
028
029import org.ametys.cms.content.ContentHelper;
030import org.ametys.cms.repository.Content;
031import org.ametys.plugins.repository.AmetysRepositoryException;
032import org.ametys.web.repository.content.WebContentDAO;
033
034/**
035 * Abstract DAO for manipulating sitemap element
036 */
037public abstract class AbstractSitemapElementsDAO extends AbstractLogEnabled implements Serviceable
038{
039    /** The content helper */
040    protected ContentHelper _contentHelper;
041    /** The content dao */
042    protected WebContentDAO _contentDAO;
043
044    public void service(ServiceManager manager) throws ServiceException
045    {
046        _contentHelper = (ContentHelper) manager.lookup(ContentHelper.ROLE);
047        _contentDAO = (WebContentDAO) manager.lookup(WebContentDAO.ROLE);
048    }
049
050    /**
051     * Sitemap Element of type container to json
052     * @param sitemapElement The page
053     * @param zoneName The zone name
054     * @param zoneItemId The zone items id
055     * @return The properties of the container
056     */
057    protected Map<String, Object> _sitemapElement2json(SitemapElement sitemapElement, String zoneName, String zoneItemId)
058    {
059        Map<String, Object> infos = new HashMap<>();
060        
061        infos.put("template", sitemapElement.getTemplate());
062        
063        List<Map<String, Object>> zones = new ArrayList<>();
064        for (Zone zone : sitemapElement.getZones())
065        {
066            if (StringUtils.isBlank(zoneName) || zoneName.equals(zone.getName()))
067            {
068                zones.add(_zone2json(zone, zoneItemId));
069            }
070        }
071        infos.put("zones", zones);
072        
073        return infos;
074    }
075    
076    private Map<String, Object> _zone2json (Zone zone, String zoneItemId)
077    {
078        Map<String, Object> jsonObject = new HashMap<>();
079        jsonObject.put("name", zone.getName());
080        jsonObject.put("isModifiable", zone instanceof ModifiableZone);
081        
082        List<Map<String, Object>> zoneitems = new ArrayList<>();
083        for (ZoneItem zoneItem : zone.getZoneItems())
084        {
085            if (StringUtils.isBlank(zoneItemId) || zoneItemId.equals(zoneItem.getId()))
086            {
087                zoneitems.add(_zoneitem2json(zoneItem));
088                
089            }
090        }
091        
092        jsonObject.put("zoneitems", zoneitems);
093        
094        return jsonObject;
095    }
096    
097    private Map<String, Object> _zoneitem2json (ZoneItem zoneItem)
098    {
099        Map<String, Object> jsonObject = new HashMap<>();
100        jsonObject.put("name", zoneItem.getName());
101        jsonObject.put("id", zoneItem.getId());
102        jsonObject.put("isModifiable", zoneItem instanceof ModifiableZoneItem);
103        
104        switch (zoneItem.getType())
105        {
106            case CONTENT:
107                try
108                {
109                    
110                    Content content = zoneItem.getContent();
111                    List<String> unknownContentTypeIds = _contentHelper.getUnknownContentTypeIds(content);
112                    if (unknownContentTypeIds.isEmpty())
113                    {
114                        jsonObject.put("viewName", zoneItem.getViewName());
115                        jsonObject.put("content", _contentDAO.getContentProperties(content));
116                    }
117                    else
118                    {
119                        getLogger().error("Unable to get content property on zone item because following content types do not exist: " + StringUtils.join(unknownContentTypeIds, ", "));
120                    }
121                }
122                catch (AmetysRepositoryException e)
123                {
124                    getLogger().error("Unable to get content property on zone item", e);
125                }
126                break;
127                
128            case SERVICE:
129                jsonObject.put("service", zoneItem.getServiceId());
130                break;
131            default:
132                break;
133        }
134        
135        return jsonObject;
136    }
137}