<commit_before>package br.net.mirante.singular.view.page.form.crud;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.inject.Inject;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.behavior.Behavior;
import org.apache.wicket.feedback.FencedFeedbackPanel;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.string.StringValue;

import br.net.mirante.singular.dao.form.ExampleDataDAO;
import br.net.mirante.singular.dao.form.ExampleDataDTO;
import br.net.mirante.singular.dao.form.FileDao;
import br.net.mirante.singular.dao.form.TemplateRepository;
import br.net.mirante.singular.form.mform.MInstancia;
import br.net.mirante.singular.form.mform.MTipo;
import br.net.mirante.singular.form.mform.document.SDocument;
import br.net.mirante.singular.form.mform.io.MformPersistenciaXML;
import br.net.mirante.singular.form.util.xml.MElement;
import br.net.mirante.singular.form.util.xml.MParser;
import br.net.mirante.singular.form.validation.InstanceValidationContext;
import br.net.mirante.singular.form.validation.ValidationErrorLevel;
import br.net.mirante.singular.form.wicket.UIBuilderWicket;
import br.net.mirante.singular.form.wicket.WicketBuildContext;
import br.net.mirante.singular.form.wicket.model.MInstanceRootModel;
import br.net.mirante.singular.form.wicket.util.WicketFormUtils;
import br.net.mirante.singular.util.wicket.bootstrap.layout.BSContainer;
import br.net.mirante.singular.util.wicket.bootstrap.layout.BSGrid;
import br.net.mirante.singular.view.SingularWicketContainer;
import br.net.mirante.singular.view.page.form.crud.services.SpringServiceRegistry;
import br.net.mirante.singular.view.template.Content;

@SuppressWarnings("serial")
public class FormContent extends Content 
                        implements SingularWicketContainer<CrudContent, Void> {

    @Inject ExampleDataDAO dao;
    @Inject FileDao filePersistence;
    @Inject SpringServiceRegistry serviceRegistry;
    private BSGrid container = new BSGrid("generated");
    private Form<?> inputForm = new Form<>("save-form");
    private MInstanceRootModel<MInstancia> currentInstance;
    private ExampleDataDTO currentModel;
    
    
//    @Inject FileDao filePersistence;
    
//    private ServiceRef<IAttachmentPersistenceHandler> temporaryRef = new ServiceRef<IAttachmentPersistenceHandler>() {
//        public IAttachmentPersistenceHandler get() {
//            return new InMemoryAttachmentPersitenceHandler();
//        }
//    };
//    
//    private ServiceRef<IAttachmentPersistenceHandler> persistanceRef = new ServiceRef<IAttachmentPersistenceHandler>() {
//        public IAttachmentPersistenceHandler get() {
//            return filePersistence;
//        }
//    };
    
//    private ServiceRef<MOptionsProvider> choiceRef = new ServiceRef<MOptionsProvider>() {
//        public MOptionsProvider get() {
//            return new MFileIdsOptionsProvider(filePersistence);
//        }
//    };
    
    public FormContent(String id, StringValue type, StringValue key) {
        super(id, false, true);
        String typeName = type.toString();
        loadOrCreateModel(key, typeName);
        currentModel.setType(typeName);
    }

    private void loadOrCreateModel(StringValue key, String typeName) {
        if(key.isEmpty()){
            currentModel = new ExampleDataDTO(UUID.randomUUID().toString());
        }else{
            currentModel = dao.find(key.toString(),typeName);
        }
        currentModel.setType(typeName);
        createInstance(typeName);
        updateContainer();
    }
    
    private void createInstance(String nomeDoTipo) {
        MTipo<?> tipo = TemplateRepository.get().loadType(nomeDoTipo);
        currentInstance = new MInstanceRootModel<MInstancia>(tipo.novaInstancia());
        bindDefaultServices(currentInstance.getObject().getDocument());
        populateInstance(tipo);
    }

    private void bindDefaultServices(SDocument document) {
//        document.setAttachmentPersistenceHandler(temporaryRef);
//        document.bindLocalService(SDocument.FILE_PERSISTENCE_SERVICE, persistanceRef);
//        document.bindLocalService("filesChoiceProvider", choiceRef);
//        document.setServiceRegistry(createSpringRegistry());
        document.addServiceRegistry(serviceRegistry);
    }

    private void populateInstance(final MTipo<?> tipo) {
        if (currentModel.getXml() == null)
            return;
        try {
            MElement xml = MParser.parse(currentModel.getXml());
            MInstancia instance = MformPersistenciaXML.fromXML(tipo, xml);
            currentInstance = new MInstanceRootModel<MInstancia>(instance);<commit_msg>Fix service bind<commit_after>package br.net.mirante.singular.view.page.form.crud;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.inject.Inject;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.behavior.Behavior;
import org.apache.wicket.feedback.FencedFeedbackPanel;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.string.StringValue;

import br.net.mirante.singular.dao.form.ExampleDataDAO;
import br.net.mirante.singular.dao.form.ExampleDataDTO;
import br.net.mirante.singular.dao.form.FileDao;
import br.net.mirante.singular.dao.form.TemplateRepository;
import br.net.mirante.singular.form.mform.MInstancia;
import br.net.mirante.singular.form.mform.MTipo;
import br.net.mirante.singular.form.mform.ServiceRef;
import br.net.mirante.singular.form.mform.core.attachment.IAttachmentPersistenceHandler;
import br.net.mirante.singular.form.mform.core.attachment.handlers.InMemoryAttachmentPersitenceHandler;
import br.net.mirante.singular.form.mform.document.SDocument;
import br.net.mirante.singular.form.mform.io.MformPersistenciaXML;
import br.net.mirante.singular.form.util.xml.MElement;
import br.net.mirante.singular.form.util.xml.MParser;
import br.net.mirante.singular.form.validation.InstanceValidationContext;
import br.net.mirante.singular.form.validation.ValidationErrorLevel;
import br.net.mirante.singular.form.wicket.UIBuilderWicket;
import br.net.mirante.singular.form.wicket.WicketBuildContext;
import br.net.mirante.singular.form.wicket.model.MInstanceRootModel;
import br.net.mirante.singular.form.wicket.util.WicketFormUtils;
import br.net.mirante.singular.util.wicket.bootstrap.layout.BSContainer;
import br.net.mirante.singular.util.wicket.bootstrap.layout.BSGrid;
import br.net.mirante.singular.view.SingularWicketContainer;
import br.net.mirante.singular.view.page.form.crud.services.SpringServiceRegistry;
import br.net.mirante.singular.view.template.Content;

@SuppressWarnings("serial")
public class FormContent extends Content 
                        implements SingularWicketContainer<CrudContent, Void> {

    @Inject ExampleDataDAO dao;
    @Inject FileDao filePersistence;
    @Inject SpringServiceRegistry serviceRegistry;
    private BSGrid container = new BSGrid("generated");
    private Form<?> inputForm = new Form<>("save-form");
    private MInstanceRootModel<MInstancia> currentInstance;
    private ExampleDataDTO currentModel;
    
    
//    @Inject FileDao filePersistence;
    
    private ServiceRef<IAttachmentPersistenceHandler> temporaryRef = new ServiceRef<IAttachmentPersistenceHandler>() {
        public IAttachmentPersistenceHandler get() {
            return new InMemoryAttachmentPersitenceHandler();
        }
    };
    
    private ServiceRef<IAttachmentPersistenceHandler> persistanceRef = new ServiceRef<IAttachmentPersistenceHandler>() {
        public IAttachmentPersistenceHandler get() {
            return filePersistence;
        }
    };
    
//    private ServiceRef<MOptionsProvider> choiceRef = new ServiceRef<MOptionsProvider>() {
//        public MOptionsProvider get() {
//            return new MFileIdsOptionsProvider(filePersistence);
//        }
//    };
    
    public FormContent(String id, StringValue type, StringValue key) {
        super(id, false, true);
        String typeName = type.toString();
        loadOrCreateModel(key, typeName);
        currentModel.setType(typeName);
    }

    private void loadOrCreateModel(StringValue key, String typeName) {
        if(key.isEmpty()){
            currentModel = new ExampleDataDTO(UUID.randomUUID().toString());
        }else{
            currentModel = dao.find(key.toString(),typeName);
        }
        currentModel.setType(typeName);
        createInstance(typeName);
        updateContainer();
    }
    
    private void createInstance(String nomeDoTipo) {
        MTipo<?> tipo = TemplateRepository.get().loadType(nomeDoTipo);
        currentInstance = new MInstanceRootModel<MInstancia>(tipo.novaInstancia());
        bindDefaultServices(currentInstance.getObject().getDocument());
        populateInstance(tipo);
    }

    private void bindDefaultServices(SDocument document) {
        document.setAttachmentPersistenceHandler(temporaryRef);
        document.bindLocalService(SDocument.FILE_PERSISTENCE_SERVICE, 
            IAttachmentPersistenceHandler.class, persistanceRef);
//        document.bindLocalService("filesChoiceProvider", choiceRef);
//        document.setServiceRegistry(createSpringRegistry());
        document.addServiceRegistry(serviceRegistry);
    }

    private void populateInstance(final MTipo<?> tipo) {
        if (currentModel.getXml() == null)
            return;
        try {
            MElement xml = MParser.parse(currentModel.getXml());
            MInstancia instance = MformPersistenciaXML.fromXML(tipo, xml);
            currentInstance = new MInstanceRootModel<MInstancia>(instance);
{'fraction_non_alphanumeric': 0.10649750096116878, 'fraction_numerical': 0.0, 'mean_word_length': 4.038740920096853, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '3441344', 'n_tokens_mistral': 3040, 'n_tokens_neox': 3042, 'n_words': 452}