001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.functions.cache;
020
021import java.io.IOException;
022import java.util.Iterator;
023
024import lucee.commons.io.cache.Cache;
025import lucee.runtime.PageContext;
026import lucee.runtime.config.ConfigImpl;
027import lucee.runtime.exp.ApplicationException;
028import lucee.runtime.exp.PageException;
029import lucee.runtime.ext.function.Function;
030import lucee.runtime.op.Caster;
031import lucee.runtime.op.Decision;
032import lucee.runtime.type.Array;
033import lucee.runtime.type.util.ListUtil;
034
035/**
036 * 
037 */
038public final class CacheRemove implements Function {
039        
040        private static final long serialVersionUID = -5823359978885018762L;
041        
042        public static String call(PageContext pc, Object ids) throws PageException {
043                return call(pc, ids, false,null);
044        }
045        public static String call(PageContext pc, Object ids, boolean throwOnError) throws PageException {
046                return call(pc, ids, throwOnError, null);
047        }
048        
049        
050        public static String call(PageContext pc, Object ids, boolean throwOnError, String cacheName) throws PageException {
051                Array arr = toArray(ids);//
052                Iterator it = arr.valueIterator();
053                String id;
054                Cache cache;
055                try {
056                        cache = Util.getCache(pc,cacheName,ConfigImpl.CACHE_DEFAULT_OBJECT);
057                } catch (IOException e) {
058                        throw Caster.toPageException(e);
059                }
060                StringBuilder sb=null;
061                try{
062                        while(it.hasNext()){
063                                id= Util.key(Caster.toString(it.next()));
064                                if(!cache.remove(id) && throwOnError){
065                                        if(sb==null)sb=new StringBuilder();
066                                        else sb.append(',');
067                                        sb.append(id);
068                                }               
069                        }
070                } 
071                catch (IOException e) {}
072                if(throwOnError && sb!=null)
073                        throw new ApplicationException("can not remove the elements with the following id(s) ["+sb+"]");
074                return null;
075        }
076        private static Array toArray(Object oIds) throws PageException {
077                if(Decision.isArray(oIds)){
078                        return Caster.toArray(oIds);
079                }
080                return ListUtil.listToArray(Caster.toString(oIds), ',');
081        }
082        
083}