Skip to content

Add query service generation and providing it to data provider #462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@
<internalFileTemplate name="Magento Data Model Interface"/>
<internalFileTemplate name="Magento Module Declarative Schema XML"/>
<internalFileTemplate name="Magento Module Declarative Schema Whitelist JSON"/>
<internalFileTemplate name="Magento Get List Query Model"/>

<defaultLiveTemplates file="/liveTemplates/MagentoPWA.xml"/>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
#parse("PHP File Header.php")

namespace ${NAMESPACE};

#set($uses = ${USES})
#foreach ($use in $uses.split(","))
use $use;
#end

/**
* Get ${ENTITY_NAME} list by search criteria query.
*/
class ${CLASS_NAME}
{
/**
* @var ${COLLECTION_PROCESSOR_TYPE}
*/
private $collectionProcessor;

/**
* @var ${ENTITY_COLLECTION_FACTORY_TYPE}
*/
private $entityCollectionFactory;

/**
* @var ${ENTITY_DATA_MAPPER_TYPE}
*/
private $entityDataMapper;

/**
* @var ${SEARCH_CRITERIA_BUILDER_TYPE}
*/
private $searchCriteriaBuilder;

/**
* @var ${SEARCH_RESULT_FACTORY_TYPE}
*/
private $searchResultFactory;

/**
* @param ${COLLECTION_PROCESSOR_TYPE} $collectionProcessor
* @param ${ENTITY_COLLECTION_FACTORY_TYPE} $entityCollectionFactory
* @param ${ENTITY_DATA_MAPPER_TYPE} $entityDataMapper
* @param ${SEARCH_CRITERIA_BUILDER_TYPE} $searchCriteriaBuilder
* @param ${SEARCH_RESULT_FACTORY_TYPE} $searchResultFactory
*/
public function __construct(
${COLLECTION_PROCESSOR_TYPE} $collectionProcessor,
${ENTITY_COLLECTION_FACTORY_TYPE} $entityCollectionFactory,
${ENTITY_DATA_MAPPER_TYPE} $entityDataMapper,
${SEARCH_CRITERIA_BUILDER_TYPE} $searchCriteriaBuilder,
${SEARCH_RESULT_FACTORY_TYPE} $searchResultFactory
) {
$this->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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<html lang="en">
<body>
<p face="verdana" size="-1">

</p>

<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
<tr>
<td colspan="3"><font face="verdana" size="-1">Template's predefined variables:</font></td>
</tr>
<tr>
<td valign="top"><nobr><font face="verdana" size="-2"><b>${NAMESPACE}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1"></font></td>
</tr>
</table>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,14 +1,91 @@
<?php
#parse("PHP File Header.php")
#if (${NAMESPACE})

namespace ${NAMESPACE};
#end

use Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider;
#set($uses = ${USES})
#foreach ($use in $uses.split(","))
use $use;
#end

class ${CLASS_NAME} extends DataProvider
#if(${HAS_GET_LIST_QUERY} == "true")
#set($hasGetListQuery = true)
#else
#set($hasGetListQuery = false)
#end
/**
* DataProvider component.
*/
class ${CLASS_NAME} extends ${EXTENDS}
{
#if($hasGetListQuery)
/**
* @var ${GET_LIST_QUERY_TYPE}
*/
private $getListQuery;

/**
* @var ${SEARCH_RESULT_FACTORY}
*/
private $searchResultFactory;

/**
* @param string $name
* @param string $primaryFieldName
* @param string $requestFieldName
* @param ${REPORTING_TYPE} $reporting
* @param ${SEARCH_CRITERIA_BUILDER} $searchCriteriaBuilder
* @param ${REQUEST_TYPE} $request
* @param ${FILTER_BUILDER} $filterBuilder
* @param ${GET_LIST_QUERY_TYPE} $getListQuery
* @param ${SEARCH_RESULT_FACTORY} $searchResultFactory
* @param array $meta
* @param array $data
*/
public function __construct(
$name,
$primaryFieldName,
$requestFieldName,
${REPORTING_TYPE} $reporting,
${SEARCH_CRITERIA_BUILDER} $searchCriteriaBuilder,
${REQUEST_TYPE} $request,
${FILTER_BUILDER} $filterBuilder,
${GET_LIST_QUERY_TYPE} $getListQuery,
${SEARCH_RESULT_FACTORY} $searchResultFactory,
array $meta = [],
array $data = []
) {
parent::__construct(
$name,
$primaryFieldName,
$requestFieldName,
$reporting,
$searchCriteriaBuilder,
$request,
$filterBuilder,
$meta,
$data
);
$this->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
*/
Expand All @@ -19,4 +96,5 @@ class ${CLASS_NAME} extends DataProvider
[]
];
}
#end
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -54,4 +73,13 @@ public String getNamespace() {
public String getPath() {
return path;
}

/**
* Get entity id field name.
*
* @return String
*/
public String getEntityIdFieldName() {
return entityIdFieldName;
}
}
Loading