Problem with query in SuggestionPickerField

Hi

I create custom component, code like below

package com.company.erp.web;

import javax.annotation.Nullable;
import javax.inject.Inject;

import org.springframework.core.Ordered;

import com.haulmont.cuba.core.global.DataManager;
import com.haulmont.cuba.core.global.LoadContext;
import com.haulmont.cuba.core.global.QueryUtils;
import com.haulmont.cuba.gui.components.Component;
import com.haulmont.cuba.gui.components.ComponentGenerationContext;
import com.haulmont.cuba.gui.components.ComponentGenerationStrategy;
import com.haulmont.cuba.gui.components.SuggestionPickerField;
import com.haulmont.cuba.gui.xml.layout.ComponentsFactory;

@org.springframework.stereotype.Component(SearchPickerComponentGenerationStrategy.NAME)
public class SearchPickerComponentGenerationStrategy implements ComponentGenerationStrategy, Ordered {

	public static final String NAME = "SearchPickerComponentGenerationStrategy";

	@Inject
	private ComponentsFactory factory;

	@Inject
	private DataManager dataManager;

	@Nullable
	@Override
	public Component createComponent(ComponentGenerationContext context) {
		String property = context.getProperty();
		if (context.getXmlDescriptor() == null)
			return null;
		String search = context.getXmlDescriptor().attributeValue("searchpicker");
		if ((search != null) && context.getComponentClass() != null) {

			SuggestionPickerField searchPickerField = factory.createComponent(SuggestionPickerField.class);
			searchPickerField.setDatasource(context.getDatasource(), property);
			String = "select e from erp$DocTypes e where e.code like :search";

			
			searchPickerField.setSearchExecutor((searchString, searchParams) -> {
				searchString = QueryUtils.escapeForLike(searchString);
				return dataManager.loadList(LoadContext.create(context.getMetaClass().getJavaClass())
						.setQuery(LoadContext.createQuery(query).setParameter("search", "%" + searchString + "%")));
			});
			// searchPickerField.setOptionsDatasource(context.getOptionsDatasource());
			searchPickerField.addLookupAction();
			searchPickerField.addClearAction();
			return searchPickerField;
		}
		return null;
	}

	@Override
	public int getOrder() {
		return HIGHEST_PLATFORM_PRECEDENCE;
	}
}

I get exception. I don’t understand, what I’m doing wrong.

java.lang.IllegalStateException: Illegal parameter info 'search'
	at com.haulmont.cuba.core.global.filter.ParametersHelper.parse(ParametersHelper.java:114)
	at com.haulmont.cuba.core.global.filter.ParametersHelper.parse(ParametersHelper.java:108)
	at com.haulmont.cuba.core.global.filter.ParametersHelper.parseQuery(ParametersHelper.java:81)
	at com.haulmont.cuba.gui.data.impl.AbstractCollectionDatasource.setQuery(AbstractCollectionDatasource.java:166)
	at com.haulmont.cuba.gui.data.impl.AbstractCollectionDatasource.setQuery(AbstractCollectionDatasource.java:119)
	at com.haulmont.cuba.gui.xml.data.DsContextLoader.loadQuery(DsContextLoader.java:411)
	at com.haulmont.cuba.gui.xml.data.DsContextLoader.loadCollectionDatasource(DsContextLoader.java:394)
	at com.haulmont.cuba.gui.xml.data.DsContextLoader.loadDatasources(DsContextLoader.java:109)
	at com.haulmont.cuba.gui.WindowManager.loadDsContext(WindowManager.java:667)
	at com.haulmont.cuba.gui.WindowManager.createWindow(WindowManager.java:565)
	at com.haulmont.cuba.gui.WindowManager.openEditor(WindowManager.java:874)
	at com.haulmont.cuba.web.WebWindowManager.openEditor(WebWindowManager.java:178)
	at com.haulmont.cuba.gui.components.WindowDelegate.openEditor(WindowDelegate.java:260)
	at com.haulmont.cuba.web.gui.WebWindow.openEditor(WebWindow.java:497)
	at com.haulmont.cuba.gui.components.actions.EditAction.internalOpenEditor(EditAction.java:274)
	at com.haulmont.cuba.gui.components.actions.EditAction.actionPerform(EditAction.java:223)
	at com.haulmont.cuba.web.gui.components.WebButton.performAction(WebButton.java:46)

I find first problem but now I have exception like below

obraz

I resolved this problem myself.

Hi,

It would be great if you not only solve your problem but also share your solution. It can help other community members!

Hi

I wrong find MetaClass from query. Now I do it like below

QueryParser parser = QueryTransformerFactory.createParser(query);
String entityName = parser.getEntityName();
MetaClass metaClass = getMetaClassByName(entityName);

and find MetaClass by method `getMetaClassByName

private MetaClass getMetaClassByName(String name) {
    MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME);
    Collection<MetaClass> mclist = metadataTools.getAllPersistentMetaClasses();
    for (MetaClass mc : mclist) {
    			if (name.equals(mc.getName())) {
    				return mc;
    			}
    		}
    		return null;
    	}