<commit_before>/*
 * Copyright 2008-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.broadleafcommerce.openadmin.client.presenter.entity;

import com.google.gwt.user.client.Timer;
import com.smartgwt.client.widgets.form.fields.FormItem;
import org.broadleafcommerce.openadmin.client.callback.SearchItemSelected;
import org.broadleafcommerce.openadmin.client.callback.SearchItemSelectedHandler;
import org.broadleafcommerce.openadmin.client.datasource.dynamic.AbstractDynamicDataSource;
import org.broadleafcommerce.openadmin.client.datasource.dynamic.DynamicEntityDataSource;
import org.broadleafcommerce.openadmin.client.dto.ForeignKey;
import org.broadleafcommerce.openadmin.client.dto.PersistencePerspective;
import org.broadleafcommerce.common.presentation.client.PersistencePerspectiveItemType;
import org.broadleafcommerce.openadmin.client.view.dynamic.dialog.EntitySearchDialog;
import org.broadleafcommerce.openadmin.client.view.dynamic.form.DynamicFormDisplay;

import java.util.HashMap;

/**
 * 
 * @author jfischer
 *
 */
public class FormItemCallbackHandlerManager {
	
	private static final long serialVersionUID = 1L;
	
	protected HashMap<String, FormItemCallback> callbacks = new HashMap<String, FormItemCallback>();

    public void addSearchFormItemCallback(String fieldName, final EntitySearchDialog searchView, final String searchDialogTitle, final DynamicFormDisplay dynamicFormDisplay) {
        addSearchFormItemCallback(fieldName, searchView, searchDialogTitle, dynamicFormDisplay, null);
    }

    public void addSearchFormItemCallback(String fieldName, final EntitySearchDialog searchView, final String searchDialogTitle, final DynamicFormDisplay dynamicFormDisplay, final ForeignKey foreignKey, final FormItemCallback cb) {
        callbacks.put(fieldName, new FormItemCallback() {
            public void execute(final FormItem formItem) {
                searchView.search(searchDialogTitle, new SearchItemSelectedHandler() {
                    public void onSearchItemSelected(SearchItemSelected event) {
                        final String myId = ((AbstractDynamicDataSource) event.getDataSource()).getPrimaryKeyValue(event.getRecord());
                        String displayFieldName = "name";
                        if (foreignKey != null) {
                            displayFieldName = foreignKey.getDisplayValueProperty();
                        } else {
                            PersistencePerspective persistencePerspective = ((DynamicEntityDataSource) dynamicFormDisplay.getFormOnlyDisplay().getForm().getDataSource()).getPersistencePerspective();
                            ForeignKey mainForeignKey = (ForeignKey) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY);
                            if (mainForeignKey != null && mainForeignKey.getManyToField().equals(formItem.getName())) {
                                displayFieldName = mainForeignKey.getDisplayValueProperty();
                            } else {
                                ForeignKey[] additionalKeys = persistencePerspective.getAdditionalForeignKeys();
                                if (additionalKeys != null) {
                                    for (ForeignKey foreignKey : additionalKeys) {
                                        if (foreignKey.getManyToField().equals(formItem.getName())) {
                                            displayFieldName = foreignKey.getDisplayValueProperty();
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                        String myName = event.getRecord().getAttribute(displayFieldName);
                        formItem.getForm().getField("__display_"+formItem.getName()).setValue(myName);
                        Timer timer = new Timer() {
                            public void run() {
                                formItem.setValue(myId);
                                dynamicFormDisplay.getSaveButton().enable();
                                dynamicFormDisplay.getRefreshButton().enable();
                                if (cb != null) {
                                    cb.execute(formItem);
                                }
                            }
                        };
                        timer.schedule(100);
                    }
                });
            }
        });<commit_msg>Modified api to allow passing datasource and to allow not specifying a dynamicFormDisplay<commit_after>/*
 * Copyright 2008-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.broadleafcommerce.openadmin.client.presenter.entity;

import com.google.gwt.user.client.Timer;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.widgets.form.fields.FormItem;
import org.broadleafcommerce.common.presentation.client.PersistencePerspectiveItemType;
import org.broadleafcommerce.openadmin.client.callback.SearchItemSelected;
import org.broadleafcommerce.openadmin.client.callback.SearchItemSelectedHandler;
import org.broadleafcommerce.openadmin.client.datasource.dynamic.AbstractDynamicDataSource;
import org.broadleafcommerce.openadmin.client.datasource.dynamic.DynamicEntityDataSource;
import org.broadleafcommerce.openadmin.client.dto.ForeignKey;
import org.broadleafcommerce.openadmin.client.dto.PersistencePerspective;
import org.broadleafcommerce.openadmin.client.view.dynamic.dialog.EntitySearchDialog;
import org.broadleafcommerce.openadmin.client.view.dynamic.form.DynamicFormDisplay;

import java.util.HashMap;

/**
 * 
 * @author jfischer
 *
 */
public class FormItemCallbackHandlerManager {
	
	private static final long serialVersionUID = 1L;
	
	protected HashMap<String, FormItemCallback> callbacks = new HashMap<String, FormItemCallback>();

    public void addSearchFormItemCallback(String fieldName, final EntitySearchDialog searchView, final String searchDialogTitle, final DynamicFormDisplay dynamicFormDisplay) {
        addSearchFormItemCallback(fieldName, searchView, searchDialogTitle, dynamicFormDisplay, null);
    }

    public void addSearchFormItemCallback(String fieldName, final EntitySearchDialog searchView, final String searchDialogTitle, final DynamicFormDisplay dynamicFormDisplay, final ForeignKey foreignKey, final FormItemCallback cb, final DataSource dataSource) {
        callbacks.put(fieldName, new FormItemCallback() {
            public void execute(final FormItem formItem) {
                searchView.search(searchDialogTitle, new SearchItemSelectedHandler() {
                    public void onSearchItemSelected(SearchItemSelected event) {
                        final String myId = ((AbstractDynamicDataSource) event.getDataSource()).getPrimaryKeyValue(event.getRecord());
                        String displayFieldName = "name";
                        if (foreignKey != null) {
                            displayFieldName = foreignKey.getDisplayValueProperty();
                        } else {
                            PersistencePerspective persistencePerspective = ((DynamicEntityDataSource) dataSource).getPersistencePerspective();
                            ForeignKey mainForeignKey = (ForeignKey) persistencePerspective.getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.FOREIGNKEY);
                            if (mainForeignKey != null && mainForeignKey.getManyToField().equals(formItem.getName())) {
                                displayFieldName = mainForeignKey.getDisplayValueProperty();
                            } else {
                                ForeignKey[] additionalKeys = persistencePerspective.getAdditionalForeignKeys();
                                if (additionalKeys != null) {
                                    for (ForeignKey foreignKey : additionalKeys) {
                                        if (foreignKey.getManyToField().equals(formItem.getName())) {
                                            displayFieldName = foreignKey.getDisplayValueProperty();
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                        String myName = event.getRecord().getAttribute(displayFieldName);
                        formItem.getForm().getField("__display_"+formItem.getName()).setValue(myName);
                        Timer timer = new Timer() {
                            public void run() {
                                formItem.setValue(myId);
                                if (dynamicFormDisplay != null) {
                                    dynamicFormDisplay.getSaveButton().enable();
                                    dynamicFormDisplay.getRefreshButton().enable();
                                    if (cb != null) {
                                        cb.execute(formItem);
                                    }
                                }
                            }
                        };
                        timer.schedule(100);
                    }
                });
            }
        });
    }

    public void addSearchFormItemCallback(String fieldName, final EntitySearchDialog searchView, final String searchDialogTitle, final DynamicFormDisplay dynamicFormDisplay, final ForeignKey foreignKey, final FormItemCallback cb) {
        addSearchFormItemCallback(fieldName, searchView, searchDialogTitle, dynamicFormDisplay, foreignKey, cb, dynamicFormDisplay.getFormOnlyDisplay().getForm().getDataSource());
{'fraction_non_alphanumeric': 0.07410145061443223, 'fraction_numerical': 0.0029566663586805876, 'mean_word_length': 2.1639871382636655, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5534139', 'n_tokens_mistral': 2402, 'n_tokens_neox': 2286, 'n_words': 536}