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 */
016package org.ametys.plugins.site.cache.monitoring.process.access.impl;
017
018import java.sql.Timestamp;
019import java.util.HashMap;
020import java.util.Map;
021
022import org.apache.commons.lang.StringUtils;
023
024import org.ametys.plugins.site.cache.monitoring.Constants;
025import org.ametys.plugins.site.cache.monitoring.process.access.ResourceAccess;
026import org.ametys.plugins.site.cache.monitoring.process.access.ResourceAccessUtils;
027
028/**
029 * Front resource access. Represent an access to a resource from the Front-office.
030 */
031public class FrontResourceAccess implements ResourceAccess
032{
033    private final String _uniqueID;
034    private final String _internalUuid;
035    private final String _site;
036    private final String _path;
037    private final String _pathHash;
038    private boolean _cacheable;
039    private boolean _cacheHit1;
040    private boolean _cacheHit2;
041    
042    /**
043     * Constructor
044     * @param uniqueID the id of the front resource access
045     * @param internalUuid the internal uuid the resource
046     * @param site the involved site
047     * @param path the path of the resource
048     */
049    public FrontResourceAccess(String uniqueID, String internalUuid, String site, String path)
050    {
051        _uniqueID = StringUtils.defaultIfEmpty(uniqueID, "-");
052        _internalUuid = StringUtils.defaultIfEmpty(internalUuid, "-");
053        _site = StringUtils.defaultIfEmpty(site, "-");
054        
055        // Calculate a hash for the path.
056        _path = StringUtils.defaultIfEmpty(StringUtils.substringBefore(path, "?"), "-");
057        _pathHash = ResourceAccessUtils.toHash(_path);
058    }
059    
060    @Override
061    public String getInsertStatementId()
062    {
063        return "FrontCacheMonitoringAccess.insertFrontResourceAccess";
064    }
065    
066    @Override
067    public Map<String, Object> getInsertStatementParameters()
068    {
069        Map<String, Object> params = new HashMap<>();
070        
071        params.put("tableName", Constants.SQL_TABLE_NAME_FRONT_ACCESS);
072        
073        params.put("Unique_Id", _uniqueID);
074        params.put("Internal_Uuid", _internalUuid);
075        params.put("Site", _site);
076        params.put("Ametys_Path_Hash", _pathHash);
077        params.put("Ametys_Path", _path);
078        params.put("Cacheable", _cacheable);
079        params.put("Cache_Hit_1", _cacheHit1);
080        params.put("Cache_Hit_2", _cacheHit2);
081        params.put("Created_At", new Timestamp(System.currentTimeMillis()));
082        
083        return params;
084    }
085    
086    /**
087     * Set the resource as cacheable or not.
088     * @param cacheable is the front esource access cacheable ?
089     */
090    public void setCacheable(boolean cacheable)
091    {
092        _cacheable = cacheable;
093    }
094    
095    /**
096     * Set the first cache hit to true/false
097     * @param hit the value to set to the first cache hit
098     */
099    public void setCacheHit1(boolean hit)
100    {
101        _cacheHit1 = hit;
102    }
103    
104    /**
105     * Set the second cache hit to true/false
106     * @param hit the value to set to the second cache hit
107     */
108    public void setCacheHit2(boolean hit)
109    {
110        _cacheHit2 = hit;
111    }
112}