/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package buzzerproxy; import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; import java.util.Set; import java.util.TreeSet; /** * CacheManager.java - beinhaltet alle Funktione zum Cachen eines Suchbegriffs * und seiner Results * * @author Sebastian Enger * @see Serialization * @since 1.1 * @version 1.1 / 2011-08-21 * @typ Class * */ public class CacheManager implements Serializable { /** * */ private static final long serialVersionUID = 1L; /** Cache */ private HashMap> cache; /** Cache Deleter */ private CacheDeleter cacheDeleter; /** Cache Writer/Reader*/ private Serialization ser; /** * Konstruktor * @throws Exception */ public CacheManager() throws Exception { cache = new HashMap>(); ser = new Serialization( Constant.CACHEFILE ); } public void setCache(HashMap> c) { this.cache = c; } public void addCache(HashMap> c) { this.cache.putAll( c ); } public HashMap> getCache() { return this.cache; } // not tested public synchronized HashMap> sortByFilename(ArrayList rCT, String filetypes) { HashMap> c = new HashMap>(); String[] typeSplit = filetypes.replaceAll( "\\.", "" ).split( "+" ); // StringBuffer sb = new StringBuffer(); for ( String type : typeSplit ) { ArrayList a = new ArrayList(); for ( ResultContainer r : rCT ) { String ft = r.getFileTyp(); if ( type.contains( ft ) ) { // filetyp des containers und der gesuchte filetype sind // gleich a.add( r ); } } c.put( type, a ); } return c; } /** * @param searchquery * @param filetypes * = .mp3+.wav+.flac * @return */ public synchronized ArrayList getResultContainerFor(String searchquery, String filetypes) { return getResultContainerFor( searchquery, filetypes, false ); } /** * @param searchquery * @param filetypes * = .mp3+.wav+.flac * @return */ private synchronized ArrayList getResultContainerFor(String searchquery, String filetypes, boolean onlyCache) { if ( filetypes == null ) throw new NullPointerException(); ArrayList res = new ArrayList(); String[] typeSplit = filetypes.split( "+" ); Set mediatypes = new TreeSet(); // für die neue Abfrage for ( String type : typeSplit ) { // Kontrolle der Abfrage (Madonna:.mp3) if ( cache.containsKey( searchquery + ":" + type ) ) { res.addAll( cache.get( searchquery + ":" + type ) ); } else { Filetype ft = Filetype.getFileType( type ); if ( ft != null ) mediatypes.add( ft.getType() ); } } if ( onlyCache ) return res; // Evt Google nochmal abfragen if ( mediatypes.size() > 0 ) { Google g = new Google(); GoogleResultParser gr = new GoogleResultParser(); StringBuilder newFileTypes = new StringBuilder(); // Anhand der Media Typen werden hier die Google Abfragen erstellt // Jeder Media Type hat viele Filetypen while ( mediatypes.iterator().hasNext() ) { Filetype[] fts = Filetype.getFilesTypeForMediaType( mediatypes.iterator().next() ); if ( fts != null && fts.length > 0 ) { for ( Filetype tf : fts ) { newFileTypes.append( tf.getCaption() ); if ( fts[fts.length - 1] != tf ) newFileTypes.append( "+" ); } } } // Google Abfrage // Alte Version: ArrayList unsortedResultBox = null; String googleResultString = g.getGoogleContent( searchquery, newFileTypes.toString() ); unsortedResultBox = gr.parseResults( googleResultString, newFileTypes.toString() ); // // Neue Version - muss noch getestet werden // ArrayList unsortedResultBox = null; // ArrayList googleResultString = g.getGoogleContentMoreResults(searchquery, newFileTypes.toString() ); // unsortedResultBox = gr.parseMoreResults(googleResultString, newFileTypes.toString() ); // Cache neu füllen fillCache( unsortedResultBox, mediatypes, searchquery ); return getResultContainerFor( searchquery, filetypes, true ); } return res; } /** * @param unsortedResultBox * @param mediatypes */ private synchronized void fillCache(ArrayList unsortedResultBox, Set mediatypes, String searchquery) { while ( mediatypes.iterator().hasNext() ) { Filetype[] fts = Filetype.getFilesTypeForMediaType( mediatypes.iterator().next() ); if ( fts != null && fts.length > 0 ) { for ( Filetype tf : fts ) { ArrayList rList = null; if ( cache.containsKey( searchquery + ":." + tf.getCaption() ) ) rList = cache.get( searchquery + ":." + tf.getCaption() ); else rList = new ArrayList(); rList.addAll( getByFiletype( tf, unsortedResultBox ) ); cache.put( searchquery + ":." + tf.getCaption(), rList ); } } } } /** * @param filetype * @param unsortedResultBox * @return */ private synchronized List getByFiletype(Filetype filetype, ArrayList unsortedResultBox) { List res = new ArrayList(); for ( ResultContainer rc : unsortedResultBox ) if ( rc.getFileTyp().equals( "." + filetype.getCaption() ) || rc.getFileTyp().equals( filetype.getCaption() ) ) res.add( rc ); return res; } }