001/* 002 * Copyright 2015 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.odfweb.catalog; 017 018import java.io.File; 019import java.io.FileFilter; 020import java.util.ArrayList; 021import java.util.HashMap; 022import java.util.List; 023import java.util.Map; 024 025import org.apache.avalon.framework.context.Context; 026import org.apache.avalon.framework.context.ContextException; 027import org.apache.avalon.framework.context.Contextualizable; 028import org.apache.avalon.framework.parameters.Parameters; 029import org.apache.cocoon.Constants; 030import org.apache.cocoon.acting.AbstractAction; 031import org.apache.cocoon.environment.ObjectModelHelper; 032import org.apache.cocoon.environment.Redirector; 033import org.apache.cocoon.environment.Request; 034import org.apache.cocoon.environment.SourceResolver; 035import org.apache.commons.lang.StringUtils; 036 037import org.ametys.core.cocoon.JSonReader; 038 039/** 040 * Get available InDesign template files from the current skin 041 */ 042public class GetInDesignTemplatesAction extends AbstractAction implements Contextualizable 043{ 044 private org.apache.cocoon.environment.Context _cocoonContext; 045 046 public void contextualize(Context context) throws ContextException 047 { 048 _cocoonContext = (org.apache.cocoon.environment.Context) context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT); 049 } 050 051 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 052 { 053 Request request = ObjectModelHelper.getRequest(objectModel); 054 055 Map<String, Object> result = new HashMap<>(); 056 057 List<Map<String, Object>> templates = new ArrayList<>(); 058 059 String skinName = request.getParameter("skinName"); 060 if (StringUtils.isNotEmpty(skinName)) 061 { 062 String indesignDirPath = _cocoonContext.getRealPath("/skins/" + skinName + "/indesign"); 063 File indesignDir = new File (indesignDirPath); 064 065 if (indesignDir.exists() && indesignDir.isDirectory()) 066 { 067 File[] models = indesignDir.listFiles(new FileFilter() 068 { 069 public boolean accept(File file) 070 { 071 return file.isFile() && file.getName().toLowerCase().endsWith(".indt"); 072 } 073 }); 074 075 for (File model : models) 076 { 077 String fileName = model.getName(); 078 079 Map<String, Object> info = new HashMap<>(); 080 info.put("filepath", fileName); 081 info.put("filename", fileName.substring(0, fileName.lastIndexOf("."))); 082 083 templates.add(info); 084 } 085 } 086 } 087 088 result.put("templates", templates); 089 090 request.setAttribute(JSonReader.OBJECT_TO_READ, result); 091 return EMPTY_MAP; 092 } 093}