Skip to content

Commit 608a819

Browse files
authored
Merge pull request #607 from bohdan-harniuk/entity-web-api-generation
Entity web api generation
2 parents 2c59bfb + 96fe0d0 commit 608a819

File tree

31 files changed

+779
-24
lines changed

31 files changed

+779
-24
lines changed

resources/fileTemplates/internal/Magento Delete Entity By Id Command.php.ft

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ class ${CLASS_NAME}
4949
* @param int $entityId
5050
*
5151
* @return void
52-
* @throws ${COULD_NOT_DELETE}|${NO_SUCH_ENTITY_EXCEPTION}
52+
* @throws ${COULD_NOT_DELETE}
5353
*/
54-
public function execute(int $entityId)
54+
public function execute(int $entityId): void
5555
{
5656
try {
5757
/** @var ${MODEL} $model */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
* ${ENTITY_NAME} entity search result.
13+
*/
14+
interface ${CLASS_NAME} extends ${PARENT_INTERFACE}
15+
{
16+
/**
17+
* Set items.
18+
*
19+
* @param ${DTO_TYPE}[] $items
20+
*
21+
* @return ${CLASS_NAME}
22+
*/
23+
public function setItems(array $items): ${CLASS_NAME};
24+
25+
/**
26+
* Get items.
27+
*
28+
* @return ${DTO_TYPE}[]
29+
*/
30+
public function getItems(): array;
31+
}

resources/fileTemplates/internal/Magento Entity Search Results Interface.php.html

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
* ${ENTITY_NAME} entity search results implementation.
13+
*/
14+
class ${CLASS_NAME} extends ${PARENT_CLASS_NAME} implements ${INTERFACE_NAME}
15+
{
16+
/**
17+
* @inheritDoc
18+
*/
19+
public function setItems(array $items): ${INTERFACE_NAME}
20+
{
21+
return parent::setItems($items);
22+
}
23+
24+
/**
25+
* @inheritDoc
26+
*/
27+
public function getItems(): array
28+
{
29+
return parent::getItems();
30+
}
31+
}

resources/fileTemplates/internal/Magento Entity Search Results.php.html

Whitespace-only changes.

resources/fileTemplates/internal/Magento Save Entity Command Model.php.ft

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class ${CLASS_NAME}
4646
/**
4747
* Save ${ENTITY_NAME}.
4848
*
49-
* @param ${DTO}|${DATA_OBJECT} $${DTO_PROPERTY}
49+
* @param ${DTO} $${DTO_PROPERTY}
5050
*
5151
* @return int
5252
* @throws ${COULD_NOT_SAVE}
@@ -74,6 +74,6 @@ class ${CLASS_NAME}
7474
throw new ${COULD_NOT_SAVE}(__('Could not save ${ENTITY_NAME}.'));
7575
}
7676

77-
return (int) $model->getEntityId();
77+
return (int) $model->getData(${ENTITY_ID_CONST});
7878
}
7979
}

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class GetListQueryModelData {
1414
private final String modelName;
1515
private final String collectionName;
1616
private final String aclResource;
17+
private final boolean hasWebApi;
1718

1819
/**
1920
* Query Model DTO Constructor.
@@ -23,19 +24,22 @@ public class GetListQueryModelData {
2324
* @param modelName String
2425
* @param collectionName String
2526
* @param aclResource String
27+
* @param hasWebApi boolean
2628
*/
2729
public GetListQueryModelData(
2830
final @NotNull String moduleName,
2931
final @NotNull String entityName,
3032
final @NotNull String modelName,
3133
final @NotNull String collectionName,
32-
final @NotNull String aclResource
34+
final @NotNull String aclResource,
35+
final boolean hasWebApi
3336
) {
3437
this.moduleName = moduleName;
3538
this.entityName = entityName;
3639
this.modelName = modelName;
3740
this.collectionName = collectionName;
3841
this.aclResource = aclResource;
42+
this.hasWebApi = hasWebApi;
3943
}
4044

4145
/**
@@ -82,4 +86,13 @@ public String getCollectionName() {
8286
public String getAclResource() {
8387
return aclResource;
8488
}
89+
90+
/**
91+
* Check if entity has Web API.
92+
*
93+
* @return boolean
94+
*/
95+
public boolean isHasWebApi() {
96+
return hasWebApi;
97+
}
8598
}

src/com/magento/idea/magento2plugin/actions/generation/data/converter/newentitydialog/GetListQueryDtoConverter.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public GetListQueryDtoConverter(
2929
newEntityDialogData.getEntityName(),
3030
newEntityDialogData.getEntityName().concat("Model"),
3131
newEntityDialogData.getEntityName().concat("Collection"),
32-
newEntityDialogData.getAclId()
32+
newEntityDialogData.getAclId(),
33+
newEntityDialogData.hasWebApi()
3334
);
3435
}
3536
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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.converter.newentitydialog;
7+
8+
import com.magento.idea.magento2plugin.actions.generation.data.converter.DataObjectConverter;
9+
import com.magento.idea.magento2plugin.actions.generation.data.dialog.EntityCreatorContextData;
10+
import com.magento.idea.magento2plugin.actions.generation.data.dialog.NewEntityDialogData;
11+
import com.magento.idea.magento2plugin.actions.generation.data.php.SearchResultsData;
12+
import org.jetbrains.annotations.NotNull;
13+
14+
public class SearchResultsDtoConverter extends SearchResultsData implements DataObjectConverter {
15+
16+
/**
17+
* Save entity command DTO converter.
18+
*
19+
* @param generationContextData EntityCreatorContextData
20+
* @param newEntityDialogData NewEntityDialogData
21+
*/
22+
public SearchResultsDtoConverter(
23+
final @NotNull EntityCreatorContextData generationContextData,
24+
final @NotNull NewEntityDialogData newEntityDialogData
25+
) {
26+
super(
27+
generationContextData.getModuleName(),
28+
newEntityDialogData.getEntityName(),
29+
newEntityDialogData.hasDtoInterface()
30+
? generationContextData.getDtoInterfaceNamespaceBuilder().getClassFqn()
31+
: generationContextData.getDtoModelNamespaceBuilder().getClassFqn()
32+
);
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.php;
7+
8+
import org.jetbrains.annotations.NotNull;
9+
10+
public class SearchResultsData {
11+
12+
private final String moduleName;
13+
private final String entityName;
14+
private final String dtoType;
15+
16+
/**
17+
* Entity search results data transfer object constructor.
18+
*
19+
* @param moduleName String
20+
* @param entityName String
21+
* @param dtoType String
22+
*/
23+
public SearchResultsData(
24+
final @NotNull String moduleName,
25+
final @NotNull String entityName,
26+
final @NotNull String dtoType
27+
) {
28+
this.moduleName = moduleName;
29+
this.entityName = entityName;
30+
this.dtoType = dtoType;
31+
}
32+
33+
/**
34+
* Get module name.
35+
*
36+
* @return String
37+
*/
38+
public String getModuleName() {
39+
return moduleName;
40+
}
41+
42+
/**
43+
* Get entity name.
44+
*
45+
* @return String
46+
*/
47+
public String getEntityName() {
48+
return entityName;
49+
}
50+
51+
/**
52+
* Get entity DTO type.
53+
*
54+
* @return String
55+
*/
56+
public String getDtoType() {
57+
return dtoType;
58+
}
59+
}

src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.java

+4
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ public class NewEntityDialog extends AbstractDialog {
156156
private static final String DTO_INTERFACE_SUFFIX = "Interface";
157157
private static final String DATA_PROVIDER_SUFFIX = "DataProvider";
158158

159+
private static final String DEFAULT_MENU_SORT_ORDER = "100";
160+
159161
private static final boolean OPEN_FILES_FLAG = false;
160162

161163
@FieldValidation(rule = RuleRegistry.NOT_EMPTY, message = {NotEmptyRule.MESSAGE, ENTITY_ID})
@@ -291,6 +293,8 @@ protected void textChanged(final @NotNull DocumentEvent event) {
291293

292294
createUiComponent.addItemListener(event -> toggleUiComponentsPanel());
293295
registerTabbedPane(tabbedPane1);
296+
297+
sortOrder.setText(DEFAULT_MENU_SORT_ORDER);
294298
}
295299

296300
/**

src/com/magento/idea/magento2plugin/actions/generation/generator/GetListQueryModelGenerator.java

+19-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile;
1212
import com.magento.idea.magento2plugin.magento.files.CollectionModelFile;
1313
import com.magento.idea.magento2plugin.magento.files.EntityDataMapperFile;
14+
import com.magento.idea.magento2plugin.magento.files.SearchResultsInterfaceFile;
1415
import com.magento.idea.magento2plugin.magento.files.queries.GetListQueryFile;
1516
import com.magento.idea.magento2plugin.magento.packages.code.FrameworkLibraryType;
1617
import java.util.Properties;
@@ -100,14 +101,24 @@ protected void fillAttributes(final @NotNull Properties attributes) {
100101
.append(
101102
"SEARCH_CRITERIA_TYPE",
102103
FrameworkLibraryType.SEARCH_CRITERIA.getType()
103-
)
104-
.append(
105-
"SEARCH_RESULT_TYPE",
106-
FrameworkLibraryType.SEARCH_RESULT.getType()
107-
)
108-
.append(
109-
"SEARCH_RESULT_FACTORY_TYPE",
110-
FrameworkLibraryType.SEARCH_RESULT.getFactory()
111104
);
105+
106+
String searchResultType;
107+
String searchResultFactoryType;
108+
109+
if (data.isHasWebApi()) {
110+
final SearchResultsInterfaceFile searchResultsInterfaceFile =
111+
new SearchResultsInterfaceFile(data.getModuleName(), data.getEntityName());
112+
113+
searchResultType = searchResultsInterfaceFile.getClassFqn();
114+
searchResultFactoryType = searchResultType.concat("Factory");
115+
} else {
116+
searchResultType = FrameworkLibraryType.SEARCH_RESULT.getType();
117+
searchResultFactoryType = FrameworkLibraryType.SEARCH_RESULT.getFactory();
118+
}
119+
120+
typesBuilder
121+
.append("SEARCH_RESULT_TYPE", searchResultType)
122+
.append("SEARCH_RESULT_FACTORY_TYPE", searchResultFactoryType);
112123
}
113124
}

src/com/magento/idea/magento2plugin/actions/generation/generator/SaveEntityCommandGenerator.java

-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ protected void fillAttributes(final @NotNull Properties attributes) {
7070
.append("ENTITY_NAME", data.getEntityName(), false)
7171
.append("CLASS_NAME", SaveEntityCommandFile.CLASS_NAME, false)
7272
.append("EXCEPTION", "Exception")
73-
.append("DATA_OBJECT", FrameworkLibraryType.DATA_OBJECT.getType())
7473
.append("COULD_NOT_SAVE", ExceptionType.COULD_NOT_SAVE.getType())
7574
.append("LOGGER", FrameworkLibraryType.LOGGER.getType());
7675

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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.generator.php;
7+
8+
import com.intellij.openapi.project.Project;
9+
import com.magento.idea.magento2plugin.actions.generation.data.php.SearchResultsData;
10+
import com.magento.idea.magento2plugin.actions.generation.generator.PhpFileGenerator;
11+
import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile;
12+
import com.magento.idea.magento2plugin.magento.files.SearchResultsFile;
13+
import com.magento.idea.magento2plugin.magento.files.SearchResultsInterfaceFile;
14+
import com.magento.idea.magento2plugin.magento.packages.code.FrameworkLibraryType;
15+
import java.util.Properties;
16+
import org.jetbrains.annotations.NotNull;
17+
18+
public class SearchResultsGenerator extends PhpFileGenerator {
19+
20+
private final SearchResultsData data;
21+
22+
/**
23+
* Search results file generator constructor.
24+
*
25+
* @param data SearchResultsData
26+
* @param project Project
27+
*/
28+
public SearchResultsGenerator(
29+
final @NotNull SearchResultsData data,
30+
final @NotNull Project project
31+
) {
32+
this(data, project, true);
33+
}
34+
35+
/**
36+
* Search results file generator constructor.
37+
*
38+
* @param data SearchResultsData
39+
* @param project Project
40+
* @param checkFileAlreadyExists boolean
41+
*/
42+
public SearchResultsGenerator(
43+
final @NotNull SearchResultsData data,
44+
final @NotNull Project project,
45+
final boolean checkFileAlreadyExists
46+
) {
47+
super(project, checkFileAlreadyExists);
48+
this.data = data;
49+
}
50+
51+
@Override
52+
protected AbstractPhpFile initFile() {
53+
return new SearchResultsFile(data.getModuleName(), data.getEntityName());
54+
}
55+
56+
@Override
57+
protected void fillAttributes(final @NotNull Properties attributes) {
58+
final String interfaceClassFqn = new SearchResultsInterfaceFile(
59+
data.getModuleName(),
60+
data.getEntityName()
61+
).getClassFqn();
62+
63+
typesBuilder
64+
.append("NAMESPACE", file.getNamespace(), false)
65+
.append("ENTITY_NAME", data.getEntityName(), false)
66+
.append("CLASS_NAME", file.getClassName(), false)
67+
.append("INTERFACE_NAME", interfaceClassFqn)
68+
.append(
69+
"PARENT_CLASS_NAME",
70+
FrameworkLibraryType.SEARCH_RESULT_IMPLEMENTATION.getType()
71+
);
72+
}
73+
}

0 commit comments

Comments
 (0)