Last active
December 21, 2015 08:39
-
-
Save iperdomo/6279918 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package services; | |
| import java.io.File; | |
| import java.io.IOException; | |
| import java.util.ArrayList; | |
| import java.util.Arrays; | |
| import java.util.List; | |
| import org.apache.commons.io.FileUtils; | |
| import com.google.appengine.api.datastore.DatastoreService; | |
| import com.google.appengine.api.datastore.DatastoreServiceFactory; | |
| import com.google.appengine.api.datastore.Entity; | |
| import com.google.appengine.api.datastore.FetchOptions; | |
| import com.google.appengine.api.datastore.PreparedQuery; | |
| 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; | |
| import com.google.appengine.api.datastore.Query.SortDirection; | |
| import com.google.appengine.tools.remoteapi.RemoteApiInstaller; | |
| import com.google.appengine.tools.remoteapi.RemoteApiOptions; | |
| @SuppressWarnings("unused") | |
| public class RemoteAPIClient { | |
| public static void main(String[] args) throws IOException, | |
| InterruptedException { | |
| String username = "[email protected]"; | |
| String password = "som3-passw0rd"; | |
| String server = "change-me-instance.appspot.com"; | |
| int port = 443; | |
| RemoteApiOptions options = new RemoteApiOptions().server(server, port) | |
| .credentials(username, password); | |
| RemoteApiInstaller installer = new RemoteApiInstaller(); | |
| installer.install(options); | |
| try { | |
| DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); | |
| RemoteAPIClient.deleteQuestionsBySurveyId(ds, 366006L); | |
| System.out.println("Done"); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } finally { | |
| installer.uninstall(); | |
| } | |
| } | |
| private static void deleteQuestionsBySurveyId (DatastoreService ds, Long id) { | |
| Filter survey = new FilterPredicate("surveyId", | |
| FilterOperator.EQUAL, id); | |
| Query q = new Query("Question").setFilter(survey); | |
| PreparedQuery pq = ds.prepare(q); | |
| List<Entity> result = pq.asList(FetchOptions.Builder.withDefaults()); | |
| for (Entity e : result) { | |
| ds.delete(e.getKey()); | |
| } | |
| } | |
| private static void fixQuestionOrder(DatastoreService ds) { | |
| Query q = new Query("Question").addSort("createdDateTime", | |
| SortDirection.DESCENDING); | |
| PreparedQuery pq = ds.prepare(q); | |
| List<Entity> result = pq.asList(FetchOptions.Builder.withDefaults()); | |
| List<Entity> fixed = new ArrayList<Entity>(); | |
| for (Entity e : result) { | |
| Long order = (Long) e.getProperty("order"); | |
| String text = (String) e.getProperty("text"); | |
| String[] parts = text.split("\\."); | |
| if (parts.length < 2) { | |
| continue; | |
| } | |
| try { | |
| Long desiredOrder = Long.valueOf(parts[0].trim()); | |
| if (!order.equals(desiredOrder)) { | |
| System.out.println("Question: " + e.getKey().getId() | |
| + " current order: " + order + " desired: " | |
| + desiredOrder); | |
| e.setProperty("order", desiredOrder); | |
| fixed.add(e); | |
| } | |
| } catch (Exception e2) { | |
| System.err.println(Arrays.toString(parts)); | |
| } | |
| } | |
| ds.put(fixed); | |
| System.out.println("Total no. questions modified: " + fixed.size()); | |
| } | |
| private static void setDefaultLanguage(DatastoreService ds) { | |
| Filter nullLang = new FilterPredicate("defaultLanguageCode", | |
| FilterOperator.EQUAL, null); | |
| Query q = new Query("Survey").setFilter(nullLang); | |
| PreparedQuery pq = ds.prepare(q); | |
| List<Entity> result = pq.asList(FetchOptions.Builder.withDefaults()); | |
| for (Entity e : result) { | |
| System.out.println("Resetting defaultLangCode for: " | |
| + e.getProperty("code")); | |
| e.setProperty("defaultLanguageCode", "en"); | |
| } | |
| ds.put(result); | |
| } | |
| private static void setDefaultPointType(DatastoreService ds) { | |
| Filter nullLang = new FilterPredicate("pointType", | |
| FilterOperator.EQUAL, null); | |
| Query q = new Query("Survey").setFilter(nullLang); | |
| PreparedQuery pq = ds.prepare(q); | |
| List<Entity> result = pq.asList(FetchOptions.Builder.withDefaults()); | |
| for (Entity e : result) { | |
| System.out.println("Resetting pointType for: " | |
| + e.getProperty("code")); | |
| e.setProperty("pointType", "Point"); | |
| } | |
| ds.put(result); | |
| } | |
| @SuppressWarnings({ "unchecked" }) | |
| private static void deleteDuplicates(DatastoreService ds) | |
| throws InterruptedException, IOException { | |
| String duplicatedFilePath = "/tmp/duplicated.txt"; | |
| List<String> duplicated = FileUtils.readLines(new File( | |
| duplicatedFilePath), "UTF-8"); | |
| int i = 0; | |
| for (String line : duplicated) { | |
| System.out.println("Processing: " + line); | |
| String[] parts = line.split("-"); | |
| Filter question = new FilterPredicate("questionID", | |
| FilterOperator.EQUAL, parts[0]); | |
| Filter instance = new FilterPredicate("surveyInstanceId", | |
| FilterOperator.EQUAL, Long.valueOf(parts[1])); | |
| Filter f = CompositeFilterOperator.and(question, instance); | |
| Query q = new Query("QuestionAnswerStore").setFilter(f).addSort( | |
| "createdDateTime", SortDirection.DESCENDING); | |
| PreparedQuery pq = ds.prepare(q); | |
| List<Entity> result = pq | |
| .asList(FetchOptions.Builder.withDefaults()); | |
| if (result.size() <= 1) { | |
| System.err.println("Skipping line: [" + line + "] contains " | |
| + result.size() + " entities"); | |
| continue; | |
| } | |
| ds.delete(result.get(0).getKey()); | |
| i++; | |
| if (i % 50 == 0) { | |
| System.out.println("Sleeping..."); | |
| Thread.sleep(5000); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment