/*
 *  Copyright (C) 2021 Stichting Akvo (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.flow.rest.handler;

import com.gallatinsystems.surveyal.dao.SurveyedLocaleDao;
import com.gallatinsystems.surveyal.domain.SurveyedLocale;
import org.waterforpeople.mapping.dao.SurveyInstanceDAO;
import org.waterforpeople.mapping.domain.SurveyInstance;

import java.util.List;
import java.util.logging.Logger;

public class FormInstanceRequestHandler {
    private static final Logger log = Logger.getLogger(FormInstanceRequestHandler.class.getSimpleName());

    private SurveyInstanceDAO siDao = new SurveyInstanceDAO();

    public void deleteFormInstance(SurveyInstance formInstance) {
        SurveyedLocaleDao surveyedLocaleDao = new SurveyedLocaleDao();
        Long surveyedLocaleId = formInstance.getSurveyedLocaleId();
        SurveyedLocale dataPoint = surveyedLocaleDao.getById(surveyedLocaleId);
        List<SurveyInstance> relatedSurveyInstances = siDao.listInstancesByLocale(surveyedLocaleId, null, null, null);
        if (relatedSurveyInstances.size() == 1
                && relatedSurveyInstances.get(0).getKey().getId() == formInstance.getKey().getId()) {
            // we need to consider what happens when a registration form is deleted even when there are
            // other existing monitoring forms. Should we delete the registration form *and* the monitoring forms
            // i.e. all the data?
            surveyedLocaleDao.delete(dataPoint);
        }
        siDao.delete(formInstance);
    }
}

@@ -1642,16 +1642,17 @@
  null);%0A
+%0A
         
@@ -1659,144 +1659,210 @@
 if (
-relatedSurveyInstances.size() == 1%0A                && relatedSurveyInstances.get(0).getKey().getId() == formInstance.getKey().getId()) %7B
+isRegistrationForm(dataPoint, formInstance)) %7B%0A            surveyedLocaleDao.delete(dataPoint);%0A            siDao.delete(relatedSurveyInstances);%0A        %7D else %7B%0A            siDao.delete(formInstance);
 %0A   
@@ -1870,56 +1870,40 @@
     
+%7D%0A
     
-// we need to consider what happens when a r
+%7D%0A%0A    private boolean isR
 egis
@@ -1913,241 +1913,178 @@
 tion
- form is deleted even when there are%0A            // other existing monitoring forms. Should we delete the registration form *and* the monitoring forms%0A            // i.e. all the data?%0A            surveyedLocaleDao.delete(dataPoint);
+Form(SurveyedLocale dataPoint, SurveyInstance formInstance) %7B%0A        return dataPoint.getCreationSurveyId() != null%0A                && formInstance.getSurveyId() != null
 %0A   
@@ -2092,18 +2092,16 @@
     
-%7D%0A
         
 siDa
@@ -2100,33 +2100,76 @@
     
-siDao.delete(formInstance
+&& dataPoint.getCreationSurveyId().equals(formInstance.getSurveyId()
 );%0A 

Delete data point and related form instances if the registration form instance is deleted