<commit_before>package ai.ilikeplaces.logic.Listeners.widgets;

import ai.ilikeplaces.doc.License;
import ai.ilikeplaces.doc.OK;
import ai.ilikeplaces.entities.PrivateLocation;
import ai.ilikeplaces.logic.Listeners.JSCodeToSend;
import ai.ilikeplaces.logic.crud.DB;
import ai.ilikeplaces.logic.validators.unit.GeoCoord;
import ai.ilikeplaces.logic.validators.unit.HumanId;
import ai.ilikeplaces.logic.validators.unit.Info;
import ai.ilikeplaces.logic.validators.unit.SimpleName;
import ai.ilikeplaces.servlets.Controller;
import ai.ilikeplaces.servlets.Controller.Page;
import ai.ilikeplaces.util.*;
import net.sf.oval.Validator;
import org.itsnat.core.ItsNatServletRequest;
import org.itsnat.core.event.NodePropertyTransport;
import org.itsnat.core.html.ItsNatHTMLDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;
import org.w3c.dom.events.Event;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;
import org.w3c.dom.html.HTMLDocument;

import static ai.ilikeplaces.servlets.Controller.Page.*;


/**
 * @author <NAME>
 */

@License(content = "This code is licensed under GNU AFFERO GENERAL PUBLIC LICENSE Version 3")
@OK
abstract public class PrivateLocationCreate extends AbstractWidgetListener {

    private static final String SAVING = "Saving...";
    RefObj<String> privateLocationName = null;

    RefObj<String> privateLocationInfo = null;

    GeoCoord woeid = null;

    HumanId humanId = null;

    final private Logger logger = LoggerFactory.getLogger(PrivateLocationCreate.class.getName());
    private static final String SAVING_PRIVATE_LOCATION = "Saving private location";
    private static final String WAS_CREATED = " was created!";

//    private static final String PRIVATE_LOCATION_CREATE_WOEIDUPDATE_TOKEN = "<PASSWORD>Location<PASSWORD>EIDUpdateToken";
//    private static final String privateLocationCreateWOEIDUpdate =
//            "\nprivateLocationCreateWOEIDUpdate = function(lat,lng){document.getElementById('" + PRIVATE_LOCATION_CREATE_WOEIDUPDATE_TOKEN + "').value = '' + lat + ',' + lng;}\n";

    /**
     * @param request__
     * @param appendToElement__
     * @param humanId__
     */
    public PrivateLocationCreate(final ItsNatServletRequest request__, final Element appendToElement__, final String humanId__) {
        super(request__, Page.PrivateLocationCreate, appendToElement__, humanId__);
    }

    /**
     *
     */
    @Override
    protected void init(final Object... initArgs) {
        this.humanId = new HumanId();
        this.woeid = new GeoCoord();
        humanId.setObjAsValid((String) initArgs[0]);

        this.privateLocationName = new SimpleName();

        this.privateLocationInfo = new Info();

        RenderWOEIDGrabber:
        {
            new WOEIDGrabber(request, $$(privateLocationCreateWOEIDGrabber), $$(privateLocationCreateWOEID));
        }
//        itsNatDocument_.addCodeToSend(
//                privateLocationCreateWOEIDUpdate.replace(
//                        PRIVATE_LOCATION_CREATE_WOEIDUPDATE_TOKEN,
//                        $$(privateLocationCreateWOEID).getAttribute(MarkupTag.GENERIC.id()))
//        );
    }

    @Override<commit_msg>[feature #16967749] Place creation should be done transparently<commit_after>package ai.ilikeplaces.logic.Listeners.widgets;

import ai.ilikeplaces.doc.License;
import ai.ilikeplaces.doc.OK;
import ai.ilikeplaces.entities.PrivateLocation;
import ai.ilikeplaces.logic.Listeners.JSCodeToSend;
import ai.ilikeplaces.logic.crud.DB;
import ai.ilikeplaces.logic.validators.unit.GeoCoord;
import ai.ilikeplaces.logic.validators.unit.HumanId;
import ai.ilikeplaces.logic.validators.unit.Info;
import ai.ilikeplaces.logic.validators.unit.SimpleName;
import ai.ilikeplaces.servlets.Controller;
import ai.ilikeplaces.servlets.Controller.Page;
import ai.ilikeplaces.util.*;
import net.sf.oval.Validator;
import org.itsnat.core.ItsNatServletRequest;
import org.itsnat.core.event.NodePropertyTransport;
import org.itsnat.core.html.ItsNatHTMLDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;
import org.w3c.dom.events.Event;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;
import org.w3c.dom.html.HTMLDocument;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

import static ai.ilikeplaces.servlets.Controller.Page.*;


/**
 * @author <NAME>
 */

@License(content = "This code is licensed under GNU AFFERO GENERAL PUBLIC LICENSE Version 3")
@OK
abstract public class PrivateLocationCreate extends AbstractWidgetListener {

    private static final String SAVING = "Saving...";
    private static final String PLACENAME = "placename";
    private static final String PLACEDETAILS = "placedetails";
    private static final String WOEHINT = "woehint";
    RefObj<String> privateLocationName = null;

    RefObj<String> privateLocationInfo = null;

    GeoCoord woeid = null;

    HumanId humanId = null;

    final private Logger logger = LoggerFactory.getLogger(PrivateLocationCreate.class.getName());
    private static final String SAVING_PRIVATE_LOCATION = "Saving private location";
    private static final String WAS_CREATED = " was created!";

//    private static final String PRIVATE_LOCATION_CREATE_WOEIDUPDATE_TOKEN = "privateLocationCreateWOEIDUpdateToken";
//    private static final String privateLocationCreateWOEIDUpdate =
//            "\nprivateLocationCreateWOEIDUpdate = function(lat,lng){document.getElementById('" + PRIVATE_LOCATION_CREATE_WOEIDUPDATE_TOKEN + "').value = '' + lat + ',' + lng;}\n";

    /**
     * @param request__
     * @param appendToElement__
     * @param humanId__
     */
    public PrivateLocationCreate(final ItsNatServletRequest request__, final Element appendToElement__, final String humanId__) {
        super(request__, Page.PrivateLocationCreate, appendToElement__, humanId__);
    }

    /**
     *
     */
    @Override
    protected void init(final Object... initArgs) {
        this.humanId = new HumanId();
        this.woeid = new GeoCoord();
        humanId.setObjAsValid((String) initArgs[0]);

        this.privateLocationName = new SimpleName();

        this.privateLocationInfo = new Info();

        Loggers.DEBUG.debug(((HttpServletRequest) request.getServletRequest()).getHeader("Referer"));

        TryToAutoCreatePrivateLocationFromURL:
        {
            TryToGetPrivateLocationNameHintFromURL:
            {
                final String placename = $$(request, PLACENAME);
                if (placename != null) {
                    this.privateLocationName.setObj(placename);
                }
            }

            TryToGetPrivateLocationDescriptionHintFromURL:
            {
                final String placedetails = $$(request, PLACEDETAILS);
                if (placedetails != null) {
                    this.privateLocationInfo.setObj(placedetails);
                }
            }

            boolean existingLocation = false;

            TryToGetPrivateLocationPositionOnMapFromURL:
            {
                if ($$(request, WOEHINT) != null) {
                    woeid = new GeoCoord().setObj($$(request, WOEHINT));
                    final List<PrivateLocation> privateLocations = DB.getHumanCRUDHumanLocal(false).doDirtyRHumansPrivateLocation(humanId).returnValue().getPrivateLocationsOwned();

                    for (final PrivateLocation privateLocation : privateLocations) {

                        if (privateLocation.getPrivateLocationLatitude() - 0.000133145585763375 < woeid.getObjectAsValid().getLatitude() &&
                                privateLocation.getPrivateLocationLatitude() + 0.000133145585763375 > woeid.getObjectAsValid().getLatitude() &&
                                privateLocation.getPrivateLocationLongitude() - 0.000184401869775 < woeid.getObjectAsValid().getLongitude() &&
                                privateLocation.getPrivateLocationLongitude() + 0.000184401869775 > woeid.getObjectAsValid().getLongitude()) {//Same place


                            itsNatDocument_.addCodeToSend(JSCodeToSend.redirectPageWithURL(
                                    new Parameter(Controller.Page.Organize.getURL())
                                            .append(Controller.Page.DocOrganizeCategory, 2, true)
                                            .append(Controller.Page.DocOrganizeLocation, privateLocation.getPrivateLocationId())
                                            .get()
                            ));
                            existingLocation = true;
                            break;
                        }
                    }

                }
            }

            DoTheCreatingPartAfterValidationPrivateLocationsCriteria:
            {
                if (this.privateLocationName.validate() == 0 && this.privateLocationInfo.validate() == 0 && this.woeid.validate() == 0) {

                    if (!existingLocation) {
                        final Return<PrivateLocation> r = DB.getHumanCrudPrivateLocationLocal(true).cPrivateLocation(
                                humanId,
                                privateLocationName,
                                privateLocationInfo,
                                woeid);
                        if (r.returnStatus() == 0) {
                            itsNatDocument_.addCodeToSend(JSCodeToSend.redirectPageWithURL(
                                    new Parameter(Controller.Page.Organize.getURL())
                                            .append(Controller.Page.DocOrganizeCategory, 2, true)
                                            .append(Controller.Page.DocOrganizeLocation, r.returnValue().getPrivateLocationId())
                                            .get()
                            ));//
                        } else {
                            $$(PrivateLocationCreateCNotice).setTextContent(r.returnMsg());
                        }
                    }
                }
            }
        }


        RenderWOEIDGrabber:
        {
            new WOEIDGrabber(request, $$(privateLocationCreateWOEIDGrabber), $$(privateLocationCreateWOEID));
        }
//        itsNatDocument_.addCodeToSend(
//                privateLocationCreateWOEIDUpdate.replace(
//                        PRIVATE_LOCATION_CREATE_WOEIDUPDATE_TOKEN,
//                        $$(privateLocationCreateWOEID).getAttribute(MarkupTag.GENERIC.id()))
//        );
    }

    @Override
{'fraction_non_alphanumeric': 0.09535591929958127, 'fraction_numerical': 0.009706889988580129, 'mean_word_length': 2.4798013245033115, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5047061', 'n_tokens_mistral': 2984, 'n_tokens_neox': 2773, 'n_words': 487}