package org.akvo.gae.remoteapi;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;;

public class EnsureHouseholdPrivacy implements Process {

	@Override
	public void execute(DatastoreService ds, String[] args) throws Exception {
		
	    if (args.length != 0) {
            System.err.println("Usage: " + RemoteAPI.class.getName()
                    + "EnsureHouseholdPrivacy <appid> <username> <password>");
            System.exit(1);
        }

        Filter projectFilter = new FilterPredicate("projectType", FilterOperator.EQUAL, "PROJECT");
        Filter publicFilter = new FilterPredicate("privacyLevel", FilterOperator.EQUAL, "PUBLIC");
        Query surveyQuery = new Query("SurveyGroup").setFilter(CompositeFilterOperator.and(projectFilter, publicFilter));

        for (Entity survey : ds.prepare(surveyQuery).asIterable()) {
            long id = survey.getKey().getId();
            Filter surveyIdFilter = new FilterPredicate("surveyGroupId", FilterOperator.EQUAL, id);
            Filter householdFilter = new FilterPredicate("pointType", FilterOperator.EQUAL, "Household");
            Query formQuery = new Query("Survey").setFilter(CompositeFilterOperator.and(surveyIdFilter, householdFilter));
            boolean containsHouseholdData = !ds.prepare(formQuery).asList(FetchOptions.Builder.withLimit(1)).isEmpty();
            if (containsHouseholdData) {
                survey.setProperty("privacyLevel", "PRIVATE");
                ds.put(survey);
                System.out.println(String.format("Survey #%s (%s) privacyLevel changed to PRIVATE",
                        id, survey.getProperty("name")));
                
            }
        }
    }
}

@@ -1,8 +1,713 @@
+/*%0A *  Copyright (C) 2015 Stichting Akvo (Akvo Foundation)%0A *%0A *  This file is part of Akvo FLOW.%0A *%0A *  Akvo FLOW is free software: you can redistribute it and modify it under the terms of%0A *  the GNU Affero General Public License (AGPL) as published by the Free Software Foundation,%0A *  either version 3 of the License or any later version.%0A *%0A *  Akvo FLOW is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;%0A *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.%0A *  See the GNU Affero General Public License included below for more details.%0A *%0A *  The full license text can also be seen at %3Chttp://www.gnu.org/licenses/agpl.html%3E.%0A */%0A%0A
 package 
@@ -1203,17 +1203,16 @@
 edicate;
-;
 %0A%0Apublic
@@ -1263,17 +1263,20 @@
 cess %7B%0A%0A
-%09
+    
 @Overrid
@@ -1277,17 +1277,20 @@
 verride%0A
-%09
+    
 public v
@@ -1360,12 +1360,13 @@
 n %7B%0A
-%09%09%0A%09
+%0A    
     
@@ -1637,24 +1637,40 @@
 rojectType%22,
+%0A               
  FilterOpera
@@ -1757,16 +1757,32 @@
 yLevel%22,
+%0A               
  FilterO
@@ -1859,16 +1859,33 @@
 yGroup%22)
+%0A                
 .setFilt
@@ -1929,16 +1929,40 @@
 tFilter,
+%0A                       
  publicF
@@ -2159,16 +2159,36 @@
 roupId%22,
+%0A                   
  FilterO
@@ -2276,16 +2276,36 @@
 ntType%22,
+%0A                   
  FilterO
@@ -2382,16 +2382,37 @@
 Survey%22)
+%0A                    
 .setFilt
@@ -2457,16 +2457,44 @@
 dFilter,
+%0A                           
  househo
@@ -2571,16 +2571,37 @@
 rmQuery)
+%0A                    
 .asList(
@@ -2831,16 +2831,41 @@
 .format(
+%0A                        
 %22Survey 
@@ -2906,16 +2906,20 @@
 RIVATE%22,
+ id,
 %0A       
@@ -2934,20 +2934,16 @@
         
- id,
  survey.
@@ -2965,32 +2965,16 @@
 me%22)));%0A
-                
 %0A       

Add license header and fix formatting