001/*
002 *  Copyright 2013 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 */
016
017package org.ametys.web.cache.monitoring.process.access.impl;
018
019import java.sql.Timestamp;
020import java.util.HashMap;
021import java.util.Map;
022
023import org.apache.commons.lang.StringUtils;
024
025import org.ametys.web.cache.monitoring.Constants;
026import org.ametys.web.cache.monitoring.process.access.ResourceAccess;
027import org.ametys.web.renderingcontext.RenderingContext;
028import org.ametys.web.repository.page.ZoneItem.ZoneType;
029
030/**
031 * Page element resource access. Represent an access to a element of a page on the back-office.
032 */
033public class PageElementResourceAccess implements ResourceAccess
034{
035    /** Type of a PageElementAccess. */
036    public enum PageElementType
037    {
038        /** A service */
039        SERVICE,
040        /** A content */
041        CONTENT,
042        /** An inputdata */
043        INPUTDATA;
044        
045        @Override
046        public String toString()
047        {
048            return super.name().toLowerCase();
049        }
050        
051        /**
052         * Utility method the get the PageElementType corresponding to a ZoneType
053         * @param zoneItemType the type of zone item
054         * @return the PageElementType
055         */
056        public static PageElementType fromZoneItemType(ZoneType zoneItemType)
057        {
058            switch (zoneItemType)
059            {
060                case CONTENT:
061                    return PageElementType.CONTENT;
062                case SERVICE:
063                    return PageElementType.SERVICE;
064                default:
065                    throw new IllegalArgumentException("Unknown ZoneType : " + zoneItemType);
066            }
067        }
068    }
069    
070    private final String _internalUuid;
071    private final String _pageID;
072    private final String _pageElementID;
073    private final PageElementType _pageElementType;
074    private final RenderingContext _renderingContext;
075    private final String _workspaceJCR;
076    private boolean _cacheable;
077    private boolean _cacheHit;
078    
079    /**
080     * Constructor
081     * @param uuid The internal UUID
082     * @param pageID the page's id
083     * @param peid the id of page element
084     * @param pet the type of page element
085     * @param rc the rendering context
086     * @param ws the JCR workspace's name
087     */
088    public PageElementResourceAccess(String uuid, String pageID, String peid, PageElementType pet, RenderingContext rc, String ws)
089    {
090        _internalUuid = StringUtils.defaultIfEmpty(uuid, "-");
091        _pageID = pageID;
092        _pageElementID = peid;
093        _pageElementType = pet;
094        _renderingContext = rc;
095        _workspaceJCR = ws;
096    }
097    
098    @Override
099    public String getInsertStatementId()
100    {
101        return "CacheMonitoringAccess.insertResourceAccessPageElement";
102    }
103    
104    @Override
105    public Map<String, Object> getInsertStatementParameters()
106    {
107        Map<String, Object> params = new HashMap<>();
108        
109        params.put("tableName", Constants.SQL_TABLE_NAME_PAGE_ELEMENTS_ACCESS);
110        
111        params.put("Internal_Uuid", _internalUuid);
112        params.put("Page_Element_Id", _pageElementID);
113        params.put("Page_Element_Type", _pageElementType.toString());
114        params.put("Page_Id", _pageID);
115        params.put("Rendering_Context", _renderingContext.toString());
116        params.put("Workspace_JCR", _workspaceJCR);
117        params.put("Cacheable", _cacheable);
118        params.put("Cache_Hit", _cacheHit);
119        params.put("Created_At", new Timestamp(System.currentTimeMillis()));
120        
121        return params;
122    }
123    
124    /**
125     * Set the resource as cacheable or not.
126     * @param cacheable true to set cacheable
127     */
128    public void setCacheable(boolean cacheable)
129    {
130        _cacheable = cacheable;
131    }
132    
133    /**
134     * Set the cache hit to true/false
135     * @param hit true to enable cache hit
136     */
137    public void setCacheHit(boolean hit)
138    {
139        _cacheHit = hit;
140    }
141}