From 61f3f000c2ec90bf298cb81e482bc7cea1a86e4d Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Mon, 25 Jan 2021 14:46:27 +0200 Subject: [PATCH 1/2] Added get list query model and injected into data provider --- resources/META-INF/plugin.xml | 1 + .../Magento Get List Query Model.php.ft | 90 ++++++++++ .../Magento Get List Query Model.php.html | 18 ++ ...omponent Custom Data Provider Class.php.ft | 86 +++++++++- .../data/GetListQueryModelData.java | 82 +++++++++ .../data/UiComponentDataProviderData.java | 28 +++ .../generation/dialog/NewEntityDialog.java | 82 ++++++++- .../generator/GetListQueryModelGenerator.java | 159 ++++++++++++++++++ .../UiComponentDataProviderGenerator.java | 100 ++++++++++- .../files/UiComponentDataProviderPhp.java | 2 + .../magento/files/queries/GetListQuery.java | 67 ++++++++ .../packages/code/FrameworkLibraryType.java | 75 +++++++++ .../GetListQuery.php | 95 +++++++++++ .../GridDataProvider.php | 3 + .../GridDataProvider.php | 85 ++++++++++ .../generator/QueryModelGeneratorTest.java | 45 +++++ ...omponentGridDataProviderGeneratorTest.java | 41 +++++ 17 files changed, 1041 insertions(+), 18 deletions(-) create mode 100644 resources/fileTemplates/internal/Magento Get List Query Model.php.ft create mode 100644 resources/fileTemplates/internal/Magento Get List Query Model.php.html create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/data/GetListQueryModelData.java create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/generator/GetListQueryModelGenerator.java create mode 100644 src/com/magento/idea/magento2plugin/magento/files/queries/GetListQuery.java create mode 100644 src/com/magento/idea/magento2plugin/magento/packages/code/FrameworkLibraryType.java create mode 100644 testData/actions/generation/generator/QueryModelGenerator/generateGetListQueryModelFile/GetListQuery.php create mode 100644 testData/actions/generation/generator/UiComponentGridDataProviderGenerator/generateDataProviderWithInjectedGetListQuery/GridDataProvider.php create mode 100644 tests/com/magento/idea/magento2plugin/actions/generation/generator/QueryModelGeneratorTest.java diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index 48e5211a0..f2537aa10 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -228,6 +228,7 @@ + diff --git a/resources/fileTemplates/internal/Magento Get List Query Model.php.ft b/resources/fileTemplates/internal/Magento Get List Query Model.php.ft new file mode 100644 index 000000000..bff40b0fd --- /dev/null +++ b/resources/fileTemplates/internal/Magento Get List Query Model.php.ft @@ -0,0 +1,90 @@ +collectionProcessor = $collectionProcessor; + $this->entityCollectionFactory = $entityCollectionFactory; + $this->entityDataMapper = $entityDataMapper; + $this->searchCriteriaBuilder = $searchCriteriaBuilder; + $this->searchResultFactory = $searchResultFactory; + } + + /** + * Get ${ENTITY_NAME} list by search criteria. + * + * @param ${SEARCH_CRITERIA_TYPE}|null $searchCriteria + * + * @return ${SEARCH_RESULT_TYPE} + */ + public function execute(?${SEARCH_CRITERIA_TYPE} $searchCriteria = null): ${SEARCH_RESULT_TYPE} + { + /** @var ${ENTITY_COLLECTION_TYPE} $collection */ + $collection = $this->entityCollectionFactory->create(); + + if ($searchCriteria === null) { + $searchCriteria = $this->searchCriteriaBuilder->create(); + } else { + $this->collectionProcessor->process($searchCriteria, $collection); + } + + $entityDataObjects = $this->entityDataMapper->map($collection); + + /** @var ${SEARCH_RESULT_TYPE} $searchResult */ + $searchResult = $this->searchResultFactory->create(); + $searchResult->setItems($entityDataObjects); + $searchResult->setTotalCount($collection->getSize()); + $searchResult->setSearchCriteria($searchCriteria); + + return $searchResult; + } +} diff --git a/resources/fileTemplates/internal/Magento Get List Query Model.php.html b/resources/fileTemplates/internal/Magento Get List Query Model.php.html new file mode 100644 index 000000000..f36b52820 --- /dev/null +++ b/resources/fileTemplates/internal/Magento Get List Query Model.php.html @@ -0,0 +1,18 @@ + + +

+ +

+ + + + + + + + + + +
Template's predefined variables:
${NAMESPACE} 
+ + diff --git a/resources/fileTemplates/internal/Magento UI Component Custom Data Provider Class.php.ft b/resources/fileTemplates/internal/Magento UI Component Custom Data Provider Class.php.ft index 38c301727..27151b44a 100644 --- a/resources/fileTemplates/internal/Magento UI Component Custom Data Provider Class.php.ft +++ b/resources/fileTemplates/internal/Magento UI Component Custom Data Provider Class.php.ft @@ -1,14 +1,91 @@ getListQuery = $getListQuery; + $this->searchResultFactory = $searchResultFactory; + } + + /** + * @inheritDoc + */ + public function getSearchResult() + { + $searchCriteria = $this->getSearchCriteria(); + $result = $this->getListQuery->execute($searchCriteria); + + return $this->searchResultFactory->create( + $result->getItems(), + $result->getTotalCount(), + $searchCriteria, + '#if(${ENTITY_ID})${ENTITY_ID}#{else}entity_id#end' + ); + } +#else /** * @inheritDoc */ @@ -19,4 +96,5 @@ class ${CLASS_NAME} extends DataProvider [] ]; } +#end } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/data/GetListQueryModelData.java b/src/com/magento/idea/magento2plugin/actions/generation/data/GetListQueryModelData.java new file mode 100644 index 000000000..41178ecbb --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/data/GetListQueryModelData.java @@ -0,0 +1,82 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.data; + +import org.jetbrains.annotations.NotNull; + +public class GetListQueryModelData { + private final String moduleName; + private final String entityName; + private final String collectionType; + private final String collectionTypeFactory; + private final String entityDataMapperType; + + /** + * Query Model DTO Constructor. + * + * @param moduleName String + * @param entityName String + * @param collectionType String + * @param entityDataMapperType String + */ + public GetListQueryModelData( + final @NotNull String moduleName, + final @NotNull String entityName, + final @NotNull String collectionType, + final @NotNull String entityDataMapperType + ) { + this.moduleName = moduleName; + this.entityName = entityName; + this.collectionType = collectionType; + this.collectionTypeFactory = collectionType.concat("Factory"); + this.entityDataMapperType = entityDataMapperType; + } + + /** + * Get Query model module name. + * + * @return String + */ + public String getModuleName() { + return moduleName; + } + + /** + * Get entity name. + * + * @return String + */ + public String getEntityName() { + return entityName; + } + + /** + * Get entity collection type. + * + * @return String + */ + public String getCollectionType() { + return collectionType; + } + + /** + * Get entity collection type factory. + * + * @return String + */ + public String getCollectionTypeFactory() { + return collectionTypeFactory; + } + + /** + * Get entity data mapper type. + * + * @return String + */ + public String getEntityDataMapperType() { + return entityDataMapperType; + } +} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/data/UiComponentDataProviderData.java b/src/com/magento/idea/magento2plugin/actions/generation/data/UiComponentDataProviderData.java index a8b518850..e82cafb20 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/data/UiComponentDataProviderData.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/data/UiComponentDataProviderData.java @@ -10,6 +10,7 @@ public class UiComponentDataProviderData { private final String name; private final String namespace; private final String path; + private final String entityIdFieldName; /** * UiComponentGridDataProviderData constructor. @@ -22,10 +23,28 @@ public UiComponentDataProviderData( final String name, final String namespace, final String path + ) { + this(name, namespace, path, null); + } + + /** + * UiComponentGridDataProviderData constructor. + * + * @param name String + * @param namespace String + * @param path String + * @param entityIdFieldName String + */ + public UiComponentDataProviderData( + final String name, + final String namespace, + final String path, + final String entityIdFieldName ) { this.name = name; this.namespace = namespace; this.path = path; + this.entityIdFieldName = entityIdFieldName; } /** @@ -54,4 +73,13 @@ public String getNamespace() { public String getPath() { return path; } + + /** + * Get entity id field name. + * + * @return String + */ + public String getEntityIdFieldName() { + return entityIdFieldName; + } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.java index 883e1d9b6..d76a29c82 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.java @@ -20,6 +20,7 @@ import com.magento.idea.magento2plugin.actions.generation.data.LayoutXmlData; import com.magento.idea.magento2plugin.actions.generation.data.MenuXmlData; import com.magento.idea.magento2plugin.actions.generation.data.ModelData; +import com.magento.idea.magento2plugin.actions.generation.data.GetListQueryModelData; import com.magento.idea.magento2plugin.actions.generation.data.ResourceModelData; import com.magento.idea.magento2plugin.actions.generation.data.RoutesXmlData; import com.magento.idea.magento2plugin.actions.generation.data.UiComponentDataProviderData; @@ -42,6 +43,7 @@ import com.magento.idea.magento2plugin.actions.generation.generator.ModuleControllerClassGenerator; import com.magento.idea.magento2plugin.actions.generation.generator.ModuleModelGenerator; import com.magento.idea.magento2plugin.actions.generation.generator.ModuleResourceModelGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.GetListQueryModelGenerator; import com.magento.idea.magento2plugin.actions.generation.generator.RoutesXmlGenerator; import com.magento.idea.magento2plugin.actions.generation.generator.UiComponentDataProviderGenerator; import com.magento.idea.magento2plugin.actions.generation.generator.UiComponentFormGenerator; @@ -54,6 +56,7 @@ import com.magento.idea.magento2plugin.magento.files.ModelPhp; import com.magento.idea.magento2plugin.magento.files.ModuleMenuXml; import com.magento.idea.magento2plugin.magento.files.ResourceModelPhp; +import com.magento.idea.magento2plugin.magento.files.UiComponentDataProviderPhp; import com.magento.idea.magento2plugin.magento.packages.Areas; import com.magento.idea.magento2plugin.magento.packages.File; import com.magento.idea.magento2plugin.magento.packages.HttpMethod; @@ -270,6 +273,7 @@ private void onOK() { generateRoutesXmlFile(); generateViewControllerFile(); generateSubmitControllerFile(); + generateModelGetListQueryFile(); generateDataProviderFile(); generateLayoutFile(); generateFormFile(); @@ -349,6 +353,11 @@ private String getDataModelName() { return getEntityName().concat("Data"); } + /** + * Get data provider class name. + * + * @return String + */ private String getDataProviderClassName() { return getEntityName().concat("DataProvider"); } @@ -381,6 +390,11 @@ private PsiFile generateResourceModelFile() { ), project).generate(ACTION_NAME, true); } + /** + * Get entity id column name. + * + * @return String + */ private String getEntityIdColumn() { return entityId.getText().trim(); } @@ -554,15 +568,26 @@ public String getAcl() { return acl.getText().trim(); } - private PsiFile generateDataProviderFile() { - final NamespaceBuilder namespace = getDataProviderNamespace(); - return new UiComponentDataProviderGenerator(new UiComponentDataProviderData( - getDataProviderClassName(), - namespace.getNamespace(), - getDataProviderDirectory() - ), getModuleName(), project).generate(ACTION_NAME, false); + /** + * Generate data provider file. + */ + private void generateDataProviderFile() { + if (getDataProviderType().equals(UiComponentDataProviderPhp.CUSTOM_TYPE)) { + final NamespaceBuilder namespaceBuilder = getDataProviderNamespace(); + new UiComponentDataProviderGenerator(new UiComponentDataProviderData( + getDataProviderClassName(), + namespaceBuilder.getNamespace(), + getDataProviderDirectory(), + getEntityIdColumn() + ), getModuleName(), project).generate(ACTION_NAME, false); + } } + /** + * Get data provider namespace builder. + * + * @return NamespaceBuilder + */ @NotNull private NamespaceBuilder getDataProviderNamespace() { return new NamespaceBuilder( @@ -572,10 +597,26 @@ private NamespaceBuilder getDataProviderNamespace() { ); } + /** + * Get data provider directory. + * + * @return String + */ public String getDataProviderDirectory() { + // TODO: add ui part with dynamic implementation. return "UI/DataProvider"; } + /** + * Get data provider type. + * + * @return String + */ + public String getDataProviderType() { + // TODO: add ui part with dynamic implementation. + return UiComponentDataProviderPhp.CUSTOM_TYPE; + } + private PsiFile generateLayoutFile() { return new LayoutXmlGenerator(new LayoutXmlData( Areas.adminhtml.toString(), @@ -940,6 +981,33 @@ private void generateWhitelistJsonFile(final @NotNull DbSchemaXmlData dbSchemaXm ).generate(ACTION_NAME, false); } + /** + * Run GetListQuery.php file generator. + */ + private void generateModelGetListQueryFile() { + final String entityCollectionType = getCollectionNamespace().getClassFqn(); + + new GetListQueryModelGenerator( + new GetListQueryModelData( + getModuleName(), + getEntityName(), + entityCollectionType, + getEntityDataMapperType() + ), + project + ).generate(ACTION_NAME, true); + } + + /** + * Get entity data mapper type. + * + * @return String + */ + private String getEntityDataMapperType() { + // TODO: implement with entity data mapper generation. + return "Test\\Test\\Mapper\\" + getEntityName() + "DataMapper"; + } + /** * Get tableResource field value. * diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/GetListQueryModelGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/GetListQueryModelGenerator.java new file mode 100644 index 000000000..ba7eb9f22 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/GetListQueryModelGenerator.java @@ -0,0 +1,159 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.generator; + +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiDirectory; +import com.intellij.psi.PsiFile; +import com.jetbrains.php.lang.psi.elements.PhpClass; +import com.magento.idea.magento2plugin.actions.generation.data.GetListQueryModelData; +import com.magento.idea.magento2plugin.actions.generation.generator.util.DirectoryGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.util.NamespaceBuilder; +import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil; +import com.magento.idea.magento2plugin.indexes.ModuleIndex; +import com.magento.idea.magento2plugin.magento.files.queries.GetListQuery; +import com.magento.idea.magento2plugin.magento.packages.code.FrameworkLibraryType; +import com.magento.idea.magento2plugin.util.GetPhpClassByFQN; +import java.util.LinkedList; +import java.util.List; +import java.util.Properties; +import org.jetbrains.annotations.NotNull; + +public class GetListQueryModelGenerator extends FileGenerator { + + private final Project project; + private final GetListQueryModelData queryModelData; + private final FileFromTemplateGenerator fileFromTemplateGenerator; + private final DirectoryGenerator directoryGenerator; + private final ModuleIndex moduleIndex; + private final NamespaceBuilder queryModelNamespaceBuilder; + private final boolean checkFileAlreadyExists; + + /** + * Query model generator Constructor. + * + * @param queryModelData QueryModelData + * @param project Project + */ + public GetListQueryModelGenerator( + final @NotNull GetListQueryModelData queryModelData, + final @NotNull Project project + ) { + this(queryModelData, project, true); + } + + /** + * Query model generator Constructor. + * + * @param queryModelData QueryModelData + * @param project Project + * @param checkFileAlreadyExists boolean + */ + public GetListQueryModelGenerator( + final @NotNull GetListQueryModelData queryModelData, + final @NotNull Project project, + final boolean checkFileAlreadyExists + ) { + super(project); + this.project = project; + this.queryModelData = queryModelData; + this.checkFileAlreadyExists = checkFileAlreadyExists; + fileFromTemplateGenerator = FileFromTemplateGenerator.getInstance(project); + directoryGenerator = DirectoryGenerator.getInstance(); + moduleIndex = ModuleIndex.getInstance(project); + queryModelNamespaceBuilder = new NamespaceBuilder( + queryModelData.getModuleName(), + GetListQuery.CLASS_NAME, + GetListQuery.DIRECTORY + ); + } + + /** + * Generate Get List Query model. + * + * @param actionName String + * + * @return PsiFile + */ + @Override + public PsiFile generate(final @NotNull String actionName) { + final PhpClass getListQueryClass = GetPhpClassByFQN.getInstance(project).execute( + GetListQuery.getClassFqn(queryModelData.getModuleName()) + ); + + if (this.checkFileAlreadyExists && getListQueryClass != null) { + return getListQueryClass.getContainingFile(); + } + + final PsiDirectory moduleBaseDir = moduleIndex.getModuleDirectoryByModuleName( + queryModelData.getModuleName() + ); + final PsiDirectory queryModelBaseDir = directoryGenerator.findOrCreateSubdirectory( + moduleBaseDir, + GetListQuery.DIRECTORY + ); + + return fileFromTemplateGenerator.generate( + GetListQuery.getInstance(), + getAttributes(), + queryModelBaseDir, + actionName + ); + } + + /** + * Fill file property attributes. + * + * @param attributes Properties + */ + @Override + protected void fillAttributes(final @NotNull Properties attributes) { + final List uses = new LinkedList<>(); + + uses.add(queryModelData.getCollectionTypeFactory()); + uses.add(queryModelData.getCollectionType()); + uses.add(queryModelData.getEntityDataMapperType()); + uses.add(FrameworkLibraryType.COLLECTION_PROCESSOR.getType()); + uses.add(FrameworkLibraryType.SEARCH_CRITERIA_BUILDER.getType()); + uses.add(FrameworkLibraryType.SEARCH_CRITERIA.getType()); + uses.add(FrameworkLibraryType.SEARCH_RESULT.getFactory()); + uses.add(FrameworkLibraryType.SEARCH_RESULT.getType()); + + attributes.setProperty("USES", PhpClassGeneratorUtil.formatUses(uses)); + attributes.setProperty("ENTITY_NAME", queryModelData.getEntityName()); + attributes.setProperty("NAMESPACE", queryModelNamespaceBuilder.getNamespace()); + attributes.setProperty("CLASS_NAME", GetListQuery.CLASS_NAME); + attributes.setProperty( + "ENTITY_COLLECTION_FACTORY_TYPE", + PhpClassGeneratorUtil.getNameFromFqn( + queryModelData.getCollectionTypeFactory() + ) + ); + attributes.setProperty( + "ENTITY_COLLECTION_TYPE", + PhpClassGeneratorUtil.getNameFromFqn( + queryModelData.getCollectionType() + ) + ); + attributes.setProperty( + "ENTITY_DATA_MAPPER_TYPE", + PhpClassGeneratorUtil.getNameFromFqn( + queryModelData.getEntityDataMapperType() + ) + ); + attributes.setProperty("COLLECTION_PROCESSOR_TYPE", + FrameworkLibraryType.COLLECTION_PROCESSOR.getTypeName()); + attributes.setProperty("SEARCH_CRITERIA_BUILDER_TYPE", + FrameworkLibraryType.SEARCH_CRITERIA_BUILDER.getTypeName()); + attributes.setProperty("SEARCH_CRITERIA_TYPE", + FrameworkLibraryType.SEARCH_CRITERIA.getTypeName()); + attributes.setProperty("SEARCH_RESULT_FACTORY_TYPE", + FrameworkLibraryType.SEARCH_RESULT.getFactoryName()); + attributes.setProperty("SEARCH_RESULT_TYPE", + FrameworkLibraryType.SEARCH_RESULT.getTypeName()); + } +} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/UiComponentDataProviderGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/UiComponentDataProviderGenerator.java index 6cdd90f5c..23832539b 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/UiComponentDataProviderGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/UiComponentDataProviderGenerator.java @@ -14,16 +14,22 @@ import com.magento.idea.magento2plugin.actions.generation.data.UiComponentDataProviderData; import com.magento.idea.magento2plugin.actions.generation.generator.util.DirectoryGenerator; import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil; import com.magento.idea.magento2plugin.bundles.CommonBundle; import com.magento.idea.magento2plugin.bundles.ValidatorBundle; import com.magento.idea.magento2plugin.indexes.ModuleIndex; import com.magento.idea.magento2plugin.magento.files.UiComponentDataProviderPhp; +import com.magento.idea.magento2plugin.magento.files.queries.GetListQuery; import com.magento.idea.magento2plugin.magento.packages.File; import com.magento.idea.magento2plugin.magento.packages.Package; +import com.magento.idea.magento2plugin.magento.packages.code.FrameworkLibraryType; import com.magento.idea.magento2plugin.util.GetFirstClassOfFile; import com.magento.idea.magento2plugin.util.GetPhpClassByFQN; +import java.util.LinkedList; +import java.util.List; import java.util.Properties; import javax.swing.JOptionPane; +import org.jetbrains.annotations.NotNull; @SuppressWarnings({"PMD.OnlyOneReturn", "PMD.DataflowAnomalyAnalysis"}) public class UiComponentDataProviderGenerator extends FileGenerator { @@ -44,12 +50,11 @@ public class UiComponentDataProviderGenerator extends FileGenerator { * @param project Project */ public UiComponentDataProviderGenerator( - final UiComponentDataProviderData uiComponentGridDataProviderData, - final String moduleName, - final Project project + final @NotNull UiComponentDataProviderData uiComponentGridDataProviderData, + final @NotNull String moduleName, + final @NotNull Project project ) { super(project); - this.uiComponentGridDataProviderData = uiComponentGridDataProviderData; this.directoryGenerator = DirectoryGenerator.getInstance(); this.fileFromTemplateGenerator = FileFromTemplateGenerator.getInstance(project); @@ -60,8 +65,15 @@ public UiComponentDataProviderGenerator( this.moduleName = moduleName; } + /** + * Generate UiComponent data provider class. + * + * @param actionName String + * + * @return PsiFile + */ @Override - public PsiFile generate(final String actionName) { + public PsiFile generate(final @NotNull String actionName) { final PsiFile[] dataProviderFiles = new PsiFile[1]; WriteCommandAction.runWriteCommandAction(project, () -> { @@ -107,13 +119,82 @@ public PsiFile generate(final String actionName) { return dataProviderFiles[0]; } + /** + * Fill file property attributes. + * + * @param attributes Properties + */ @Override - protected void fillAttributes(final Properties attributes) { + protected void fillAttributes(final @NotNull Properties attributes) { attributes.setProperty("NAMESPACE", uiComponentGridDataProviderData.getNamespace()); attributes.setProperty("CLASS_NAME", uiComponentGridDataProviderData.getName()); + + if (uiComponentGridDataProviderData.getEntityIdFieldName() != null) { + attributes.setProperty( + "ENTITY_ID", + uiComponentGridDataProviderData.getEntityIdFieldName() + ); + } + attributes.setProperty("HAS_GET_LIST_QUERY", "false"); + + final List uses = new LinkedList<>(); + + uses.add(UiComponentDataProviderPhp.DEFAULT_DATA_PROVIDER); + attributes.setProperty( + "EXTENDS", + PhpClassGeneratorUtil.getNameFromFqn( + UiComponentDataProviderPhp.DEFAULT_DATA_PROVIDER + ) + ); + + final PhpClass getListQueryFile = GetPhpClassByFQN.getInstance(project).execute( + GetListQuery.getClassFqn(moduleName) + ); + + if (getListQueryFile == null) { + attributes.setProperty("USES", PhpClassGeneratorUtil.formatUses(uses)); + return; + } + attributes.setProperty("HAS_GET_LIST_QUERY", "true"); + + uses.add(FrameworkLibraryType.REPORTING.getType()); + attributes.setProperty("REPORTING_TYPE", FrameworkLibraryType.REPORTING.getTypeName()); + + uses.add(FrameworkLibraryType.API_SEARCH_CRITERIA_BUILDER.getType()); + attributes.setProperty("SEARCH_CRITERIA_BUILDER", + FrameworkLibraryType.API_SEARCH_CRITERIA_BUILDER.getTypeName()); + + uses.add(FrameworkLibraryType.REQUEST.getType()); + attributes.setProperty("REQUEST_TYPE", FrameworkLibraryType.REQUEST.getTypeName()); + + uses.add(FrameworkLibraryType.FILTER_BUILDER.getType()); + attributes.setProperty("FILTER_BUILDER", FrameworkLibraryType.FILTER_BUILDER.getTypeName()); + + uses.add(UiComponentDataProviderPhp.SEARCH_RESULT_FACTORY); + attributes.setProperty("SEARCH_RESULT_FACTORY", + PhpClassGeneratorUtil.getNameFromFqn( + UiComponentDataProviderPhp.SEARCH_RESULT_FACTORY + ) + ); + + final @NotNull String getListQueryFqn = getListQueryFile.getPresentableFQN(); + + uses.add(getListQueryFqn); + attributes.setProperty("GET_LIST_QUERY_TYPE", PhpClassGeneratorUtil.getNameFromFqn( + getListQueryFqn + )); + + attributes.setProperty("USES", PhpClassGeneratorUtil.formatUses(uses)); } - private PhpClass createDataProviderClass(final String actionName) { + /** + * Generate data provider class. + * + * @param actionName String + * + * @return PhpClass + */ + private PhpClass createDataProviderClass(final @NotNull String actionName) { PsiDirectory parentDirectory = ModuleIndex.getInstance(project) .getModuleDirectoryByModuleName(this.moduleName); final PsiFile dataProviderFile; @@ -144,6 +225,11 @@ private PhpClass createDataProviderClass(final String actionName) { return getFirstClassOfFile.execute((PhpFile) dataProviderFile); } + /** + * Get data provider class FQN. + * + * @return String + */ private String getDataProviderFqn() { return String.format( "%s%s%s", diff --git a/src/com/magento/idea/magento2plugin/magento/files/UiComponentDataProviderPhp.java b/src/com/magento/idea/magento2plugin/magento/files/UiComponentDataProviderPhp.java index 24e72cec8..1cb3f62bf 100644 --- a/src/com/magento/idea/magento2plugin/magento/files/UiComponentDataProviderPhp.java +++ b/src/com/magento/idea/magento2plugin/magento/files/UiComponentDataProviderPhp.java @@ -22,6 +22,8 @@ public class UiComponentDataProviderPhp implements ModuleFileInterface { private String className; public static final String DEFAULT_DATA_PROVIDER = "Magento\\Framework\\View\\Element\\UiComponent\\DataProvider\\DataProvider"; + public static final String SEARCH_RESULT_FACTORY = + "Magento\\Ui\\DataProvider\\SearchResultFactory"; /** * Returns a new instance of the class. diff --git a/src/com/magento/idea/magento2plugin/magento/files/queries/GetListQuery.java b/src/com/magento/idea/magento2plugin/magento/files/queries/GetListQuery.java new file mode 100644 index 000000000..34f12c60b --- /dev/null +++ b/src/com/magento/idea/magento2plugin/magento/files/queries/GetListQuery.java @@ -0,0 +1,67 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.magento.files.queries; + +import com.intellij.lang.Language; +import com.jetbrains.php.lang.PhpLanguage; +import com.magento.idea.magento2plugin.actions.generation.generator.util.NamespaceBuilder; +import com.magento.idea.magento2plugin.magento.files.ModuleFileInterface; +import com.magento.idea.magento2plugin.magento.packages.Package; +import org.jetbrains.annotations.NotNull; + +public final class GetListQuery implements ModuleFileInterface { + public static final String DIRECTORY = "Query"; + public static final String CLASS_NAME = "GetListQuery"; + public static final String FILE_EXTENSION = "php"; + public static final String TEMPLATE = "Magento Get List Query Model"; + private static final GetListQuery INSTANCE = new GetListQuery(); + + /** + * Get singleton instance of the class. + * + * @return GetListQuery + */ + public static GetListQuery getInstance() { + return INSTANCE; + } + + /** + * Get class FQN. + * + * @param moduleName String + * + * @return String + */ + public static String getClassFqn(final @NotNull String moduleName) { + final NamespaceBuilder namespaceBuilder = new NamespaceBuilder( + moduleName, + GetListQuery.CLASS_NAME, + GetListQuery.DIRECTORY + ); + + return String.format( + "%s%s%s", + namespaceBuilder.getNamespace(), + Package.fqnSeparator, + CLASS_NAME + ); + } + + @Override + public String getFileName() { + return CLASS_NAME.concat("." + FILE_EXTENSION); + } + + @Override + public String getTemplate() { + return TEMPLATE; + } + + @Override + public Language getLanguage() { + return PhpLanguage.INSTANCE; + } +} diff --git a/src/com/magento/idea/magento2plugin/magento/packages/code/FrameworkLibraryType.java b/src/com/magento/idea/magento2plugin/magento/packages/code/FrameworkLibraryType.java new file mode 100644 index 000000000..924554396 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/magento/packages/code/FrameworkLibraryType.java @@ -0,0 +1,75 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.magento.packages.code; + +import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil; +import org.jetbrains.annotations.NotNull; + +public enum FrameworkLibraryType { + COLLECTION_PROCESSOR("Magento\\Framework\\Api\\SearchCriteria\\CollectionProcessorInterface"), + FILTER_BUILDER("Magento\\Framework\\Api\\FilterBuilder"), + REPORTING("Magento\\Framework\\Api\\Search\\ReportingInterface"), + REQUEST("Magento\\Framework\\App\\RequestInterface"), + SEARCH_CRITERIA("Magento\\Framework\\Api\\SearchCriteriaInterface"), + SEARCH_CRITERIA_BUILDER("Magento\\Framework\\Api\\SearchCriteriaBuilder"), + API_SEARCH_CRITERIA_BUILDER("Magento\\Framework\\Api\\Search\\SearchCriteriaBuilder"), + SEARCH_RESULT("Magento\\Framework\\Api\\SearchResultsInterface"); + + /** + * Factory type suffix. + */ + private static final String FACTORY_SUFFIX = "Factory"; + + /** + * Framework Library type. + */ + private final String type; + + /** + * Framework Library type ENUM constructor. + * + * @param type String + */ + FrameworkLibraryType(final @NotNull String type) { + this.type = type; + } + + /** + * Get type. + * + * @return String + */ + public @NotNull String getType() { + return type; + } + + /** + * Get name from type FQN. + * + * @return String + */ + public @NotNull String getTypeName() { + return PhpClassGeneratorUtil.getNameFromFqn(getType()); + } + + /** + * Get factory type for type. + * + * @return String + */ + public @NotNull String getFactory() { + return type.concat(FACTORY_SUFFIX); + } + + /** + * Get factory type name from factory type FQN. + * + * @return String + */ + public @NotNull String getFactoryName() { + return PhpClassGeneratorUtil.getNameFromFqn(getFactory()); + } +} diff --git a/testData/actions/generation/generator/QueryModelGenerator/generateGetListQueryModelFile/GetListQuery.php b/testData/actions/generation/generator/QueryModelGenerator/generateGetListQueryModelFile/GetListQuery.php new file mode 100644 index 000000000..dbfb79255 --- /dev/null +++ b/testData/actions/generation/generator/QueryModelGenerator/generateGetListQueryModelFile/GetListQuery.php @@ -0,0 +1,95 @@ +collectionProcessor = $collectionProcessor; + $this->entityCollectionFactory = $entityCollectionFactory; + $this->entityDataMapper = $entityDataMapper; + $this->searchCriteriaBuilder = $searchCriteriaBuilder; + $this->searchResultFactory = $searchResultFactory; + } + + /** + * Get Book list by search criteria. + * + * @param SearchCriteriaInterface|null $searchCriteria + * + * @return SearchResultsInterface + */ + public function execute(?SearchCriteriaInterface $searchCriteria = null): SearchResultsInterface + { + /** @var Collection $collection */ + $collection = $this->entityCollectionFactory->create(); + + if ($searchCriteria === null) { + $searchCriteria = $this->searchCriteriaBuilder->create(); + } else { + $this->collectionProcessor->process($searchCriteria, $collection); + } + + $entityDataObjects = $this->entityDataMapper->map($collection); + + /** @var SearchResultsInterface $searchResult */ + $searchResult = $this->searchResultFactory->create(); + $searchResult->setItems($entityDataObjects); + $searchResult->setTotalCount($collection->getSize()); + $searchResult->setSearchCriteria($searchCriteria); + + return $searchResult; + } +} diff --git a/testData/actions/generation/generator/UiComponentGridDataProviderGenerator/generateCustomDataProvider/GridDataProvider.php b/testData/actions/generation/generator/UiComponentGridDataProviderGenerator/generateCustomDataProvider/GridDataProvider.php index 9ac857ef1..9fa4fa30f 100644 --- a/testData/actions/generation/generator/UiComponentGridDataProviderGenerator/generateCustomDataProvider/GridDataProvider.php +++ b/testData/actions/generation/generator/UiComponentGridDataProviderGenerator/generateCustomDataProvider/GridDataProvider.php @@ -4,6 +4,9 @@ use Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider; +/** + * DataProvider component. + */ class GridDataProvider extends DataProvider { /** diff --git a/testData/actions/generation/generator/UiComponentGridDataProviderGenerator/generateDataProviderWithInjectedGetListQuery/GridDataProvider.php b/testData/actions/generation/generator/UiComponentGridDataProviderGenerator/generateDataProviderWithInjectedGetListQuery/GridDataProvider.php new file mode 100644 index 000000000..b5ba1faa2 --- /dev/null +++ b/testData/actions/generation/generator/UiComponentGridDataProviderGenerator/generateDataProviderWithInjectedGetListQuery/GridDataProvider.php @@ -0,0 +1,85 @@ +getListQuery = $getListQuery; + $this->searchResultFactory = $searchResultFactory; + } + + /** + * @inheritDoc + */ + public function getSearchResult() + { + $searchCriteria = $this->getSearchCriteria(); + $result = $this->getListQuery->execute($searchCriteria); + + return $this->searchResultFactory->create( + $result->getItems(), + $result->getTotalCount(), + $searchCriteria, + 'entity_id' + ); + } +} diff --git a/tests/com/magento/idea/magento2plugin/actions/generation/generator/QueryModelGeneratorTest.java b/tests/com/magento/idea/magento2plugin/actions/generation/generator/QueryModelGeneratorTest.java new file mode 100644 index 000000000..fb30875e2 --- /dev/null +++ b/tests/com/magento/idea/magento2plugin/actions/generation/generator/QueryModelGeneratorTest.java @@ -0,0 +1,45 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.generator; + +import com.magento.idea.magento2plugin.actions.generation.data.GetListQueryModelData; +import com.magento.idea.magento2plugin.magento.files.queries.GetListQuery; + +public class QueryModelGeneratorTest extends BaseGeneratorTestCase { + private static final String MODULE_NAME = "Foo_Bar"; + private static final String EXPECTED_DIRECTORY = "src/app/code/Foo/Bar/" + + GetListQuery.DIRECTORY; + private static final String ENTITY_NAME = "Book"; + private static final String COLLECTION_TYPE = "Foo\\Bar\\Model\\ResourceModel\\" + + ENTITY_NAME + "\\Collection"; + private static final String ENTITY_DATA_MAPPER_TYPE = "Foo\\Bar\\Mapper\\" + + ENTITY_NAME + "DataMapper"; + + /** + * Test generation of GetListQuery model for entity. + */ + public void testGenerateGetListQueryModelFile() { + final GetListQueryModelData getListQueryModelData = new GetListQueryModelData( + MODULE_NAME, + ENTITY_NAME, + COLLECTION_TYPE, + ENTITY_DATA_MAPPER_TYPE + ); + final GetListQueryModelGenerator getListQueryModelGenerator = + new GetListQueryModelGenerator( + getListQueryModelData, + myFixture.getProject(), + false + ); + final String filePath = this.getFixturePath(GetListQuery.getInstance().getFileName()); + + assertGeneratedFileIsCorrect( + myFixture.configureByFile(filePath), + EXPECTED_DIRECTORY, + getListQueryModelGenerator.generate("test") + ); + } +} diff --git a/tests/com/magento/idea/magento2plugin/actions/generation/generator/UiComponentGridDataProviderGeneratorTest.java b/tests/com/magento/idea/magento2plugin/actions/generation/generator/UiComponentGridDataProviderGeneratorTest.java index fa7e920b6..a1f119799 100644 --- a/tests/com/magento/idea/magento2plugin/actions/generation/generator/UiComponentGridDataProviderGeneratorTest.java +++ b/tests/com/magento/idea/magento2plugin/actions/generation/generator/UiComponentGridDataProviderGeneratorTest.java @@ -7,14 +7,20 @@ import com.intellij.openapi.project.Project; import com.intellij.psi.PsiFile; +import com.magento.idea.magento2plugin.actions.generation.data.GetListQueryModelData; import com.magento.idea.magento2plugin.actions.generation.data.UiComponentDataProviderData; public class UiComponentGridDataProviderGeneratorTest extends BaseGeneratorTestCase { private static final String EXPECTED_DIRECTORY = "src/app/code/Foo/Bar/Ui/Component/Listing"; private static final String MODULE_NAME = "Foo_Bar"; + private static final String ENTITY_NAME = "Book"; private static final String PROVIDER_CLASS_NAME = "GridDataProvider"; private static final String PROVIDER_NAMESPACE = "Foo\\Bar\\Ui\\Listing"; private static final String PROVIDER_PATH = "Ui/Component/Listing"; + private static final String COLLECTION_TYPE = "Foo\\Bar\\Model\\ResourceModel\\" + + ENTITY_NAME + "\\Collection"; + private static final String ENTITY_DATA_MAPPER_TYPE = "Foo\\Bar\\Mapper\\" + + ENTITY_NAME + "DataMapper"; /** * Test data provider class file generation with custom type. @@ -31,6 +37,27 @@ public void testGenerateCustomDataProvider() { ); } + /** + * Test data provider class file generation when get list query model exists. + */ + public void testGenerateDataProviderWithInjectedGetListQuery() { + generateGetListQuery(); + final PsiFile dataProviderFile = generateDataProvider(); + final String filePath = this.getFixturePath("GridDataProvider.php"); + final PsiFile expectedFile = myFixture.configureByFile(filePath); + + assertGeneratedFileIsCorrect( + expectedFile, + EXPECTED_DIRECTORY, + dataProviderFile + ); + } + + /** + * Generate data provider file. + * + * @return PsiFile + */ private PsiFile generateDataProvider() { final Project project = myFixture.getProject(); final UiComponentDataProviderData providerData = new UiComponentDataProviderData( @@ -47,4 +74,18 @@ private PsiFile generateDataProvider() { return generator.generate("test"); } + + /** + * Generate get list query model file. + */ + private void generateGetListQuery() { + final Project project = myFixture.getProject(); + final GetListQueryModelData getListData = new GetListQueryModelData( + MODULE_NAME, + ENTITY_NAME, + COLLECTION_TYPE, + ENTITY_DATA_MAPPER_TYPE + ); + new GetListQueryModelGenerator(getListData, project, false).generate("test"); + } } From fbb083d672d915c6364a3b78b86fad838c1f9845 Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Mon, 25 Jan 2021 15:07:56 +0200 Subject: [PATCH 2/2] Fixed tests --- .../actions/generation/dialog/NewEntityDialog.java | 4 ++-- .../generateGetListQueryModelFile/GetListQuery.php | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.java index 059867d0e..fd16ec207 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.java @@ -10,7 +10,7 @@ import com.intellij.psi.PsiDirectory; import com.intellij.psi.PsiFile; import com.intellij.util.indexing.FileBasedIndex; -import com.magento.idea.magento2plugin.actions.generation.NewViewModelAction; +import com.magento.idea.magento2plugin.actions.generation.NewEntityAction; import com.magento.idea.magento2plugin.actions.generation.OverrideClassByAPreferenceAction; import com.magento.idea.magento2plugin.actions.generation.data.AclXmlData; import com.magento.idea.magento2plugin.actions.generation.data.CollectionData; @@ -184,7 +184,7 @@ public NewEntityDialog(final Project project, final PsiDirectory directory) { setContentPane(contentPane); setModal(true); - setTitle(NewViewModelAction.ACTION_DESCRIPTION); + setTitle(NewEntityAction.ACTION_DESCRIPTION); getRootPane().setDefaultButton(buttonOK); buttonOK.addActionListener((final ActionEvent event) -> onOK()); diff --git a/testData/actions/generation/generator/QueryModelGenerator/generateGetListQueryModelFile/GetListQuery.php b/testData/actions/generation/generator/QueryModelGenerator/generateGetListQueryModelFile/GetListQuery.php index dbfb79255..4e23950a9 100644 --- a/testData/actions/generation/generator/QueryModelGenerator/generateGetListQueryModelFile/GetListQuery.php +++ b/testData/actions/generation/generator/QueryModelGenerator/generateGetListQueryModelFile/GetListQuery.php @@ -1,5 +1,4 @@