001/*
002 *  Copyright 2016 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.indexing;
017
018import java.util.List;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.quartz.JobDataMap;
024import org.quartz.JobExecutionContext;
025
026import org.ametys.core.schedule.Schedulable;
027import org.ametys.core.util.JSONUtils;
028import org.ametys.plugins.core.impl.schedule.AbstractStaticSchedulable;
029import org.ametys.plugins.core.schedule.Scheduler;
030import org.ametys.web.repository.site.Site;
031import org.ametys.web.repository.site.SiteManager;
032
033/**
034 * A {@link Schedulable} job for indexing a site.
035 */
036public class SiteIndexerSchedulable extends AbstractStaticSchedulable
037{
038    /** The key for the site to rebuild the live workspace */
039    protected static final String JOBDATAMAP_SITE_KEY = "siteName";
040    
041    private static final String __JOBDATAMAP_SITE_KEY = Scheduler.PARAM_VALUES_PREFIX + JOBDATAMAP_SITE_KEY;
042    
043    /** The site amanger */
044    protected SiteManager _siteManager;
045
046    /** The utils for JSON */
047    protected JSONUtils _jsonUtils;
048
049    /** The */
050    protected SiteIndexer _siteIndexer;
051
052    @Override
053    public void service(ServiceManager manager) throws ServiceException
054    {
055        super.service(manager);
056        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
057        _jsonUtils = (JSONUtils) manager.lookup(JSONUtils.ROLE);
058        _siteIndexer = (SiteIndexer) manager.lookup(SiteIndexer.ROLE);
059    }
060    
061    @Override
062    public void execute(JobExecutionContext context) throws Exception
063    {
064        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
065        String siteAsMap = (String) jobDataMap.get(__JOBDATAMAP_SITE_KEY);
066        Site site = _getSite(siteAsMap);
067        
068        _siteIndexer.indexSite(site);
069    }
070    
071    private Site _getSite(String siteAsMap)
072    {
073        Map<String, Object> mapSite = _jsonUtils.convertJsonToMap(siteAsMap);
074        @SuppressWarnings("unchecked")
075        List<String> sites = (List<String>) mapSite.get("sites");
076        String siteName = sites.get(0);
077        
078        return _siteManager.getSite(siteName);
079    }
080}