Skip to content

Commit 5a68b4c

Browse files
author
Vitaliy
authored
Merge pull request #462 from bohdan-harniuk/add-query-service-generation-and-providing-it-to-data-provider
Add query service generation and providing it to data provider
2 parents 3715ffc + fbb083d commit 5a68b4c

File tree

17 files changed

+1042
-20
lines changed

17 files changed

+1042
-20
lines changed

resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@
228228
<internalFileTemplate name="Magento Data Model Interface"/>
229229
<internalFileTemplate name="Magento Module Declarative Schema XML"/>
230230
<internalFileTemplate name="Magento Module Declarative Schema Whitelist JSON"/>
231+
<internalFileTemplate name="Magento Get List Query Model"/>
231232

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

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
#parse("PHP File Header.php")
3+
4+
namespace ${NAMESPACE};
5+
6+
#set($uses = ${USES})
7+
#foreach ($use in $uses.split(","))
8+
use $use;
9+
#end
10+
11+
/**
12+
* Get ${ENTITY_NAME} list by search criteria query.
13+
*/
14+
class ${CLASS_NAME}
15+
{
16+
/**
17+
* @var ${COLLECTION_PROCESSOR_TYPE}
18+
*/
19+
private $collectionProcessor;
20+
21+
/**
22+
* @var ${ENTITY_COLLECTION_FACTORY_TYPE}
23+
*/
24+
private $entityCollectionFactory;
25+
26+
/**
27+
* @var ${ENTITY_DATA_MAPPER_TYPE}
28+
*/
29+
private $entityDataMapper;
30+
31+
/**
32+
* @var ${SEARCH_CRITERIA_BUILDER_TYPE}
33+
*/
34+
private $searchCriteriaBuilder;
35+
36+
/**
37+
* @var ${SEARCH_RESULT_FACTORY_TYPE}
38+
*/
39+
private $searchResultFactory;
40+
41+
/**
42+
* @param ${COLLECTION_PROCESSOR_TYPE} $collectionProcessor
43+
* @param ${ENTITY_COLLECTION_FACTORY_TYPE} $entityCollectionFactory
44+
* @param ${ENTITY_DATA_MAPPER_TYPE} $entityDataMapper
45+
* @param ${SEARCH_CRITERIA_BUILDER_TYPE} $searchCriteriaBuilder
46+
* @param ${SEARCH_RESULT_FACTORY_TYPE} $searchResultFactory
47+
*/
48+
public function __construct(
49+
${COLLECTION_PROCESSOR_TYPE} $collectionProcessor,
50+
${ENTITY_COLLECTION_FACTORY_TYPE} $entityCollectionFactory,
51+
${ENTITY_DATA_MAPPER_TYPE} $entityDataMapper,
52+
${SEARCH_CRITERIA_BUILDER_TYPE} $searchCriteriaBuilder,
53+
${SEARCH_RESULT_FACTORY_TYPE} $searchResultFactory
54+
) {
55+
$this->collectionProcessor = $collectionProcessor;
56+
$this->entityCollectionFactory = $entityCollectionFactory;
57+
$this->entityDataMapper = $entityDataMapper;
58+
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
59+
$this->searchResultFactory = $searchResultFactory;
60+
}
61+
62+
/**
63+
* Get ${ENTITY_NAME} list by search criteria.
64+
*
65+
* @param ${SEARCH_CRITERIA_TYPE}|null $searchCriteria
66+
*
67+
* @return ${SEARCH_RESULT_TYPE}
68+
*/
69+
public function execute(?${SEARCH_CRITERIA_TYPE} $searchCriteria = null): ${SEARCH_RESULT_TYPE}
70+
{
71+
/** @var ${ENTITY_COLLECTION_TYPE} $collection */
72+
$collection = $this->entityCollectionFactory->create();
73+
74+
if ($searchCriteria === null) {
75+
$searchCriteria = $this->searchCriteriaBuilder->create();
76+
} else {
77+
$this->collectionProcessor->process($searchCriteria, $collection);
78+
}
79+
80+
$entityDataObjects = $this->entityDataMapper->map($collection);
81+
82+
/** @var ${SEARCH_RESULT_TYPE} $searchResult */
83+
$searchResult = $this->searchResultFactory->create();
84+
$searchResult->setItems($entityDataObjects);
85+
$searchResult->setTotalCount($collection->getSize());
86+
$searchResult->setSearchCriteria($searchCriteria);
87+
88+
return $searchResult;
89+
}
90+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<html lang="en">
2+
<body>
3+
<p face="verdana" size="-1">
4+
5+
</p>
6+
7+
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
8+
<tr>
9+
<td colspan="3"><font face="verdana" size="-1">Template's predefined variables:</font></td>
10+
</tr>
11+
<tr>
12+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${NAMESPACE}</b></font></nobr></td>
13+
<td width="10">&nbsp;</td>
14+
<td width="100%" valign="top"><font face="verdana" size="-1"></font></td>
15+
</tr>
16+
</table>
17+
</body>
18+
</html>
Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,91 @@
11
<?php
22
#parse("PHP File Header.php")
3-
#if (${NAMESPACE})
43

54
namespace ${NAMESPACE};
6-
#end
75

8-
use Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider;
6+
#set($uses = ${USES})
7+
#foreach ($use in $uses.split(","))
8+
use $use;
9+
#end
910

10-
class ${CLASS_NAME} extends DataProvider
11+
#if(${HAS_GET_LIST_QUERY} == "true")
12+
#set($hasGetListQuery = true)
13+
#else
14+
#set($hasGetListQuery = false)
15+
#end
16+
/**
17+
* DataProvider component.
18+
*/
19+
class ${CLASS_NAME} extends ${EXTENDS}
1120
{
21+
#if($hasGetListQuery)
22+
/**
23+
* @var ${GET_LIST_QUERY_TYPE}
24+
*/
25+
private $getListQuery;
26+
27+
/**
28+
* @var ${SEARCH_RESULT_FACTORY}
29+
*/
30+
private $searchResultFactory;
31+
32+
/**
33+
* @param string $name
34+
* @param string $primaryFieldName
35+
* @param string $requestFieldName
36+
* @param ${REPORTING_TYPE} $reporting
37+
* @param ${SEARCH_CRITERIA_BUILDER} $searchCriteriaBuilder
38+
* @param ${REQUEST_TYPE} $request
39+
* @param ${FILTER_BUILDER} $filterBuilder
40+
* @param ${GET_LIST_QUERY_TYPE} $getListQuery
41+
* @param ${SEARCH_RESULT_FACTORY} $searchResultFactory
42+
* @param array $meta
43+
* @param array $data
44+
*/
45+
public function __construct(
46+
$name,
47+
$primaryFieldName,
48+
$requestFieldName,
49+
${REPORTING_TYPE} $reporting,
50+
${SEARCH_CRITERIA_BUILDER} $searchCriteriaBuilder,
51+
${REQUEST_TYPE} $request,
52+
${FILTER_BUILDER} $filterBuilder,
53+
${GET_LIST_QUERY_TYPE} $getListQuery,
54+
${SEARCH_RESULT_FACTORY} $searchResultFactory,
55+
array $meta = [],
56+
array $data = []
57+
) {
58+
parent::__construct(
59+
$name,
60+
$primaryFieldName,
61+
$requestFieldName,
62+
$reporting,
63+
$searchCriteriaBuilder,
64+
$request,
65+
$filterBuilder,
66+
$meta,
67+
$data
68+
);
69+
$this->getListQuery = $getListQuery;
70+
$this->searchResultFactory = $searchResultFactory;
71+
}
72+
73+
/**
74+
* @inheritDoc
75+
*/
76+
public function getSearchResult()
77+
{
78+
$searchCriteria = $this->getSearchCriteria();
79+
$result = $this->getListQuery->execute($searchCriteria);
80+
81+
return $this->searchResultFactory->create(
82+
$result->getItems(),
83+
$result->getTotalCount(),
84+
$searchCriteria,
85+
'#if(${ENTITY_ID})${ENTITY_ID}#{else}entity_id#end'
86+
);
87+
}
88+
#else
1289
/**
1390
* @inheritDoc
1491
*/
@@ -19,4 +96,5 @@ class ${CLASS_NAME} extends DataProvider
1996
[]
2097
];
2198
}
99+
#end
22100
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.actions.generation.data;
7+
8+
import org.jetbrains.annotations.NotNull;
9+
10+
public class GetListQueryModelData {
11+
private final String moduleName;
12+
private final String entityName;
13+
private final String collectionType;
14+
private final String collectionTypeFactory;
15+
private final String entityDataMapperType;
16+
17+
/**
18+
* Query Model DTO Constructor.
19+
*
20+
* @param moduleName String
21+
* @param entityName String
22+
* @param collectionType String
23+
* @param entityDataMapperType String
24+
*/
25+
public GetListQueryModelData(
26+
final @NotNull String moduleName,
27+
final @NotNull String entityName,
28+
final @NotNull String collectionType,
29+
final @NotNull String entityDataMapperType
30+
) {
31+
this.moduleName = moduleName;
32+
this.entityName = entityName;
33+
this.collectionType = collectionType;
34+
this.collectionTypeFactory = collectionType.concat("Factory");
35+
this.entityDataMapperType = entityDataMapperType;
36+
}
37+
38+
/**
39+
* Get Query model module name.
40+
*
41+
* @return String
42+
*/
43+
public String getModuleName() {
44+
return moduleName;
45+
}
46+
47+
/**
48+
* Get entity name.
49+
*
50+
* @return String
51+
*/
52+
public String getEntityName() {
53+
return entityName;
54+
}
55+
56+
/**
57+
* Get entity collection type.
58+
*
59+
* @return String
60+
*/
61+
public String getCollectionType() {
62+
return collectionType;
63+
}
64+
65+
/**
66+
* Get entity collection type factory.
67+
*
68+
* @return String
69+
*/
70+
public String getCollectionTypeFactory() {
71+
return collectionTypeFactory;
72+
}
73+
74+
/**
75+
* Get entity data mapper type.
76+
*
77+
* @return String
78+
*/
79+
public String getEntityDataMapperType() {
80+
return entityDataMapperType;
81+
}
82+
}

src/com/magento/idea/magento2plugin/actions/generation/data/UiComponentDataProviderData.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class UiComponentDataProviderData {
1010
private final String name;
1111
private final String namespace;
1212
private final String path;
13+
private final String entityIdFieldName;
1314

1415
/**
1516
* UiComponentGridDataProviderData constructor.
@@ -22,10 +23,28 @@ public UiComponentDataProviderData(
2223
final String name,
2324
final String namespace,
2425
final String path
26+
) {
27+
this(name, namespace, path, null);
28+
}
29+
30+
/**
31+
* UiComponentGridDataProviderData constructor.
32+
*
33+
* @param name String
34+
* @param namespace String
35+
* @param path String
36+
* @param entityIdFieldName String
37+
*/
38+
public UiComponentDataProviderData(
39+
final String name,
40+
final String namespace,
41+
final String path,
42+
final String entityIdFieldName
2543
) {
2644
this.name = name;
2745
this.namespace = namespace;
2846
this.path = path;
47+
this.entityIdFieldName = entityIdFieldName;
2948
}
3049

3150
/**
@@ -54,4 +73,13 @@ public String getNamespace() {
5473
public String getPath() {
5574
return path;
5675
}
76+
77+
/**
78+
* Get entity id field name.
79+
*
80+
* @return String
81+
*/
82+
public String getEntityIdFieldName() {
83+
return entityIdFieldName;
84+
}
5785
}

0 commit comments

Comments
 (0)