|
| 1 | +/* |
| 2 | + * Copyright (C) 2022 Stichting Akvo (Akvo Foundation) |
| 3 | + * |
| 4 | + * This file is part of Akvo FLOW. |
| 5 | + * |
| 6 | + * Akvo FLOW is free software: you can redistribute it and modify it under the terms of |
| 7 | + * the GNU Affero General Public License (AGPL) as published by the Free Software Foundation, |
| 8 | + * either version 3 of the License or any later version. |
| 9 | + * |
| 10 | + * Akvo FLOW is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 11 | + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the GNU Affero General Public License included below for more details. |
| 13 | + * |
| 14 | + * The full license text can also be seen at <http://www.gnu.org/licenses/agpl.html>. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.akvo.gae.remoteapi; |
| 18 | + |
| 19 | +import com.google.appengine.api.datastore.DatastoreService; |
| 20 | +import com.google.appengine.api.datastore.Entity; |
| 21 | +import com.google.appengine.api.datastore.EntityNotFoundException; |
| 22 | +import com.google.appengine.api.datastore.FetchOptions; |
| 23 | +import com.google.appengine.api.datastore.Key; |
| 24 | +import com.google.appengine.api.datastore.KeyFactory; |
| 25 | +import com.google.appengine.api.datastore.PreparedQuery; |
| 26 | +import com.google.appengine.api.datastore.Query; |
| 27 | +import com.google.appengine.api.datastore.Query.Filter; |
| 28 | +import com.google.appengine.api.datastore.Query.FilterOperator; |
| 29 | +import com.google.appengine.api.datastore.Query.FilterPredicate; |
| 30 | +import com.google.appengine.api.datastore.Text; |
| 31 | + |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +/* |
| 36 | + Search for responses to a specific question and add an argument to delete all the responses |
| 37 | + for only that question |
| 38 | + */ |
| 39 | +public class ReplaceCaddisflyImage implements Process { |
| 40 | + |
| 41 | + @Override |
| 42 | + public void execute(DatastoreService ds, String[] args) throws Exception { |
| 43 | + |
| 44 | + if (args.length == 0) { |
| 45 | + System.out.println("Missing question Id"); |
| 46 | + System.exit(1); |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + final Long questionId = Long.parseLong(args[0].trim()); |
| 51 | + |
| 52 | + try { |
| 53 | + Entity question = ds.get(KeyFactory.createKey("Question", questionId)); |
| 54 | + List<Object> responseIds = getCaddisflyResponses(ds, questionId.toString()); |
| 55 | + } catch (EntityNotFoundException e) { |
| 56 | + System.out.println("Question not found: " + questionId); |
| 57 | + return; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private List<Object> getCaddisflyResponses(DatastoreService ds, String questionId) { |
| 62 | + final Filter f = new FilterPredicate("questionID", FilterOperator.EQUAL, questionId); |
| 63 | + final Query q = new Query("QuestionAnswerStore").setFilter(f); |
| 64 | + final PreparedQuery pq = ds.prepare(q); |
| 65 | + final List<Object> values = new ArrayList<>(); |
| 66 | + for (Entity e : pq.asList(FetchOptions.Builder.withDefaults())) { |
| 67 | + Text valueText = (Text) e.getProperty("valueText"); |
| 68 | + Object value = valueText.getValue(); |
| 69 | + System.out.println(value); |
| 70 | + values.add(value); |
| 71 | + } |
| 72 | + System.out.println(String.format("Found %s responses...", values.size())); |
| 73 | + return values; |
| 74 | + } |
| 75 | +} |
0 commit comments