001/** 002 * Copyright (C) 2014 Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/ 003 * 004 * This file is part of Dicoogle/dicoogle-sdk. 005 * 006 * Dicoogle/dicoogle-sdk is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * Dicoogle/dicoogle-sdk is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with Dicoogle. If not, see <http://www.gnu.org/licenses/>. 018 */ 019package pt.ua.dicoogle.sdk.utils.query; 020 021import java.util.ArrayList; 022import java.util.List; 023import java.util.concurrent.CountDownLatch; 024import java.util.concurrent.ExecutionException; 025 026import pt.ua.dicoogle.sdk.core.DicooglePlatformInterface; 027import pt.ua.dicoogle.sdk.datastructs.SearchResult; 028import pt.ua.dicoogle.sdk.task.JointQueryTask; 029import pt.ua.dicoogle.sdk.task.Task; 030 031/** 032 * This class provides a very easy interface for querying in the Dicoogle. 033 * Especially in big data scenarios where users should be aware of memory restrictions. 034 * The usage of an iterative approach is enforced by the adapter. As such, query interface users are not tempted to use the tasks bulk get method. 035 * The query's business logic should be as much encompassed in the adapter as possible. 036 * 037 * @author Tiago Marques Godinho, tmgodinho@ua.pt 038 * 039 */ 040public class Query extends JointQueryTask{ 041 042 private DicooglePlatformInterface controller; 043 044 private ForEachAdapter adapter; 045 private CountDownLatch latch; 046 047 /** 048 * Initializes this helper class. 049 * 050 * @param controller The dicoogle platform proxy. 051 * @param adapter The adapter holding the business logic of the query. 052 */ 053 public Query(DicooglePlatformInterface controller, ForEachAdapter adapter) { 054 super(); 055 this.adapter = adapter; 056 this.controller = controller; 057 this.latch = new CountDownLatch(1); 058 } 059 060 @Override 061 public void onCompletion() { 062 // TODO Auto-generated method stub 063 this.latch.countDown(); 064 } 065 066 @Override 067 public void onReceive(Task<Iterable<SearchResult>> e) { 068 069 Iterable<SearchResult> rs = null; 070 try { 071 rs = e.get(); 072 } catch (InterruptedException | ExecutionException e1) { 073 e1.printStackTrace(); 074 } 075 076 for(SearchResult r : rs){ 077 this.adapter.forEach(r); 078 } 079 } 080 081 /** 082 * Awaits for the query to finish. 083 */ 084 public void await() { 085 try { 086 latch.await(); 087 } catch (InterruptedException e) { 088 // TODO Auto-generated catch block 089 e.printStackTrace(); 090 } 091 } 092 093 /** 094 * This method provides the same interface as the Dicoogle Platform Interface. 095 * The await method is called after the query is performed. So this is a blocking method. 096 * 097 * @see DicooglePlatformInterface 098 * 099 * @param query Query String 100 * @param parameters Parameters 101 */ 102 public void queryAll(String query, 103 Object... parameters){ 104 @SuppressWarnings("unused") 105 JointQueryTask task = controller.queryAll(this, query, parameters); 106 await(); 107 } 108 109 /** 110 * This method provides the same interface as the Dicoogle Platform Interface. 111 * The await method is called after the query is performed. So this is a blocking method. 112 * 113 * @see DicooglePlatformInterface 114 * 115 * @param querySource The query plugin's name 116 * @param query Query String 117 * @param parameters Parameters 118 */ 119 public void query(String querySource, 120 String query, Object... parameters) { 121 List<String> querySources = new ArrayList<String>(1); 122 querySources.add(querySource); 123 124 this.query(querySources, query, parameters); 125 } 126 127 /** 128 * This method provides the same interface as the Dicoogle Platform Interface. 129 * The await method is called after the query is performed. So this is a blocking method. 130 * 131 * @see DicooglePlatformInterface 132 * 133 * @param querySources A List holding the queries plugin's names that should handle the query. 134 * @param query Query String 135 * @param parameters Parameters 136 */ 137 public void query(List<String> querySources, String query, Object... parameters) { 138 @SuppressWarnings("unused") 139 JointQueryTask task = controller.query(this, querySources, query, parameters); 140 141 await(); 142 } 143 144 /** 145 * Sets the Dicoogle Platform Controller. 146 * @param controller The new controller. 147 */ 148 public void setController(DicooglePlatformInterface controller) { 149 this.controller = controller; 150 } 151 152 /** 153 * Sets the for each adapter. 154 * 155 * @param adapter The adapter. 156 */ 157 public void setAdapter(ForEachAdapter adapter) { 158 this.adapter = adapter; 159 } 160}