<commit_before>/*
 *  Copyright (C) 2015 <NAME> (Akvo Foundation)
 *
 *  This file is part of Akvo FLOW.
 *
 *  Akvo FLOW is free software: you can redistribute it and modify it under the terms of
 *  the GNU Affero General Public License (AGPL) as published by the Free Software Foundation,
 *  either version 3 of the License or any later version.
 *
 *  Akvo FLOW is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU Affero General Public License included below for more details.
 *
 *  The full license text can also be seen at <http://www.gnu.org/licenses/agpl.html>.
 */

package org.akvo.gae.remoteapi;

import java.util.LinkedList;
import java.util.List;

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;

public class CorrectFolderSurveyPath implements Process {

    private final String SURVEY_GROUP = "SurveyGroup";
    private final String SURVEY = "Survey";

    @Override
    public void execute(DatastoreService ds, String[] args) throws Exception {

        System.out.println("Fetching survey groups and surveys.");

        final List<Entity> surveyGroups = ds.prepare(new Query(SURVEY_GROUP)).asList(
                FetchOptions.Builder.withDefaults());
        final List<Entity> surveys = ds.prepare(new Query(SURVEY)).asList(
                FetchOptions.Builder.withDefaults());

        // combine entities to one list
        LinkedList<Entity> completeList = new LinkedList<Entity>();
        completeList.addAll(surveyGroups);
        completeList.addAll(surveys);

        for (Entity rootLevelEntity : completeList) {
            if (rootLevelEntity.getKind().equals(SURVEY_GROUP)
                    && rootLevelEntity.getProperty("parentId") == null) {
                updateFolderSurveyPaths(rootLevelEntity, "", completeList);
            }
        }

        System.out.println("Saving " + surveyGroups.size() + " survey groups; " + surveys.size()
                + " surveys.");
        ds.put(completeList);
    }

    private void updateFolderSurveyPaths(Entity entity, String pathPrefix, List<Entity> entities) {
        // set folder/survey path
        String entityPath = String.format("%s/%s", pathPrefix, entity.getProperty("name"));
        entity.setProperty("path", entityPath);
        System.out.println("Setting path for: " + entity.getKey() + " = " + entityPath);

        // set all child folders/surveys paths
        for (Entity childEntity : entities) {
            Long parentId = getParentId(childEntity);
            if (parentId != null && parentId.equals(entity.getKey().getId())) {
                updateFolderSurveyPaths(childEntity, entityPath, entities);
            }
        }
    }

    private Long getParentId(Entity childEntity) {
        if (childEntity.getKind().equals(SURVEY_GROUP)) {
            return (Long) childEntity.getProperty("parentId");
        } else {
            return (Long) childEntity.getProperty("surveyGroupId");
        }
    }<commit_msg>[#1153] Update only incorrect path entities<commit_after>/*
 *  Copyright (C) 2015 <NAME> (Akvo Foundation)
 *
 *  This file is part of Akvo FLOW.
 *
 *  Akvo FLOW is free software: you can redistribute it and modify it under the terms of
 *  the GNU Affero General Public License (AGPL) as published by the Free Software Foundation,
 *  either version 3 of the License or any later version.
 *
 *  Akvo FLOW is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU Affero General Public License included below for more details.
 *
 *  The full license text can also be seen at <http://www.gnu.org/licenses/agpl.html>.
 */

package org.akvo.gae.remoteapi;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

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;

public class CorrectFolderSurveyPath implements Process {

    private static final String SURVEY_GROUP = "SurveyGroup";
    private static final String SURVEY = "Survey";
    private List<Entity> updatedEntities = new ArrayList<Entity>();

    @Override
    public void execute(DatastoreService ds, String[] args) throws Exception {

        System.out.println("Fetching survey groups and surveys.");

        final List<Entity> surveyGroups = ds.prepare(new Query(SURVEY_GROUP)).asList(
                FetchOptions.Builder.withDefaults());
        final List<Entity> surveys = ds.prepare(new Query(SURVEY)).asList(
                FetchOptions.Builder.withDefaults());

        // combine entities to one list
        LinkedList<Entity> completeList = new LinkedList<Entity>();
        completeList.addAll(surveyGroups);
        completeList.addAll(surveys);

        for (Entity rootLevelEntity : completeList) {
            if (rootLevelEntity.getKind().equals(SURVEY_GROUP)
                    && rootLevelEntity.getProperty("parentId") == null) {
                updateFolderSurveyPaths(rootLevelEntity, "", completeList);
            }
        }

        ds.put(updatedEntities);
    }

    private void updateFolderSurveyPaths(Entity entity, String pathPrefix, List<Entity> entities) {
        // set folder/survey path
        String currentPath = (String) entity.getProperty("path");
        String newEntityPath = String.format("%s/%s", pathPrefix, entity.getProperty("name"));
        if (newEntityPath.equals(currentPath)) {
            System.out.println("No changes made for: " + entity.getKey() + " = " + currentPath);
        } else {
            entity.setProperty("path", newEntityPath);
            updatedEntities.add(entity);
            System.out.println("Setting path for: " + entity.getKey() + "; " + currentPath + " => "
                    + newEntityPath);
        }

        // set all child folders/surveys paths
        for (Entity childEntity : entities) {
            Long parentId = getParentId(childEntity);
            if (parentId != null && parentId.equals(entity.getKey().getId())) {
                updateFolderSurveyPaths(childEntity, newEntityPath, entities);
            }
        }
    }

    private Long getParentId(Entity childEntity) {
        if (childEntity.getKind().equals(SURVEY_GROUP)) {
            return (Long) childEntity.getProperty("parentId");
        } else {
            return (Long) childEntity.getProperty("surveyGroupId");
        }
    }
{'fraction_non_alphanumeric': 0.10254907705830647, 'fraction_numerical': 0.0020509815411661296, 'mean_word_length': 2.9371395617070357, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6615388', 'n_tokens_mistral': 1870, 'n_tokens_neox': 1824, 'n_words': 509}