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.cache.monitoring.process.access.ResourceAccessUtils;
028import org.ametys.web.cache.monitoring.process.access.impl.PageElementResourceAccess.PageElementType;
029import org.ametys.web.renderingcontext.RenderingContext;
030
031/**
032 * Page resource access. Represent an access to a page on the back-office.
033 */
034public class PageResourceAccess implements ResourceAccess
035{
036    private final String _internalUuid;
037    private final String _pageID;
038    private final String _path;
039    private final String _pathHash;
040    private RenderingContext _renderingContext;
041    private String _workspaceJCR;
042    private boolean _cacheable;
043    
044    
045    /**
046     * Constructor
047     * @param internalUuid the internal UUID
048     * @param pageID The page's id
049     * @param path The page's path
050     */
051    public PageResourceAccess(String internalUuid, String pageID, String path)
052    {
053        _internalUuid = StringUtils.defaultIfEmpty(internalUuid, "-");
054        _pageID = pageID;
055        _path = StringUtils.substringBefore(path, "?");
056        
057        // Calculate a hash for the path.
058        _pathHash = ResourceAccessUtils.toHash(_path);
059    }
060    
061    @Override
062    public String getInsertStatementId()
063    {
064        return "CacheMonitoringAccess.insertResourceAccessPage";
065    }
066    
067    @Override
068    public Map<String, Object> getInsertStatementParameters()
069    {
070        Map<String, Object> params = new HashMap<>();
071        
072        params.put("tableName", Constants.SQL_TABLE_NAME_PAGE_ACCESS);
073        
074        params.put("Internal_Uuid", _internalUuid);
075        params.put("Page_Id", _pageID);
076        params.put("Page_Path_Hash", _pathHash);
077        params.put("Page_Path", _path);
078        params.put("Rendering_Context", _renderingContext.toString());
079        params.put("Workspace_JCR", _workspaceJCR);
080        params.put("Cacheable", _cacheable);
081        params.put("Created_At", new Timestamp(System.currentTimeMillis()));
082        
083        return params;
084    }
085    
086    /**
087     * Create a page element resource access for this page resource access.
088     * @param pageElementID the id of page element
089     * @param pageElementType the type of page element resource access
090     * @return the new PageElementAccess
091     */
092    public PageElementResourceAccess createPageElementAccess(String pageElementID, PageElementType pageElementType)
093    {
094        return new PageElementResourceAccess(_internalUuid, _pageID, pageElementID, pageElementType, _renderingContext, _workspaceJCR);
095    }
096    
097    /**
098     * Set the rendering context
099     * @param renderingContext the rendering context to set
100     */
101    public void setRenderingContext(RenderingContext renderingContext)
102    {
103        _renderingContext = renderingContext;
104    }
105    
106    /**
107     * Set the jcr workspace
108     * @param workspaceJCR The JCR workspace's name
109     */
110    public void setWorkspaceJCR(String workspaceJCR)
111    {
112        _workspaceJCR = workspaceJCR;
113    }
114    
115    /**
116     * Set the resource as cacheable or not.
117     * @param cacheable true to set cacheable
118     */
119    public void setCacheable(boolean cacheable)
120    {
121        _cacheable = cacheable;
122    }
123}