Skip to content

Commit 87b1022

Browse files
author
Vitaliy
authored
Merge pull request #463 from bohdan-harniuk/entity-save-command-class-generation
Added save entity command class generation
2 parents 4b3ea8b + 1e2bdff commit 87b1022

File tree

11 files changed

+687
-2
lines changed

11 files changed

+687
-2
lines changed

resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@
231231
<internalFileTemplate name="Magento Get List Query Model"/>
232232
<internalFileTemplate name="Magento Entity Save Controller Class"/>
233233
<internalFileTemplate name="Magento Entity Data Mapper"/>
234+
<internalFileTemplate name="Magento Save Entity Command Model"/>
234235

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
* Save ${ENTITY_NAME} Command.
13+
*/
14+
class ${CLASS_NAME}
15+
{
16+
/**
17+
* @var ${LOGGER}
18+
*/
19+
private $logger;
20+
21+
/**
22+
* @var ${MODEL_FACTORY}
23+
*/
24+
private $modelFactory;
25+
26+
/**
27+
* @var ${RESOURCE}
28+
*/
29+
private $resource;
30+
31+
/**
32+
* @param ${LOGGER} $logger
33+
* @param ${MODEL_FACTORY} $modelFactory
34+
* @param ${RESOURCE} $resource
35+
*/
36+
public function __construct(
37+
${LOGGER} $logger,
38+
${MODEL_FACTORY} $modelFactory,
39+
${RESOURCE} $resource
40+
) {
41+
$this->logger = $logger;
42+
$this->modelFactory = $modelFactory;
43+
$this->resource = $resource;
44+
}
45+
46+
/**
47+
* Save ${ENTITY_NAME}.
48+
*
49+
* @param ${DTO}|${DATA_OBJECT} $${DTO_PROPERTY}
50+
*
51+
* @return int
52+
* @throws ${COULD_NOT_SAVE}
53+
*/
54+
public function execute(${DTO} $${DTO_PROPERTY}): int
55+
{
56+
try {
57+
/** @var ${MODEL} $model */
58+
$model = $this->modelFactory->create();
59+
$model->addData($${DTO_PROPERTY}->getData());
60+
$this->resource->save($model);
61+
} catch (${EXCEPTION} $exception) {
62+
$this->logger->error(
63+
__('Could not save ${ENTITY_NAME}. Original message: {message}'),
64+
[
65+
'message' => $exception->getMessage(),
66+
'exception' => $exception
67+
]
68+
);
69+
throw new ${COULD_NOT_SAVE}(__('Could not save ${ENTITY_NAME}.'));
70+
}
71+
72+
return (int) $model->getEntityId();
73+
}
74+
}
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: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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 SaveEntityCommandData {
11+
private final String moduleName;
12+
private final String entityName;
13+
private final String namespace;
14+
private final String classFqn;
15+
private final String modelClassFqn;
16+
private final String resourceModelClassFqn;
17+
private final String dataModelClassFqn;
18+
19+
/**
20+
* Save Command DTO Constructor.
21+
*
22+
* @param moduleName String
23+
* @param entityName String
24+
* @param namespace String
25+
* @param classFqn String
26+
* @param modelClassFqn String
27+
* @param resourceModelClassFqn String
28+
* @param dataModelClassFqn String
29+
*/
30+
public SaveEntityCommandData(
31+
final @NotNull String moduleName,
32+
final @NotNull String entityName,
33+
final @NotNull String namespace,
34+
final @NotNull String classFqn,
35+
final @NotNull String modelClassFqn,
36+
final @NotNull String resourceModelClassFqn,
37+
final @NotNull String dataModelClassFqn
38+
) {
39+
this.moduleName = moduleName;
40+
this.entityName = entityName;
41+
this.namespace = namespace;
42+
this.classFqn = classFqn;
43+
this.modelClassFqn = modelClassFqn;
44+
this.resourceModelClassFqn = resourceModelClassFqn;
45+
this.dataModelClassFqn = dataModelClassFqn;
46+
}
47+
48+
/**
49+
* Get module name.
50+
*
51+
* @return String
52+
*/
53+
public String getModuleName() {
54+
return moduleName;
55+
}
56+
57+
/**
58+
* Get entity name.
59+
*
60+
* @return String
61+
*/
62+
public String getEntityName() {
63+
return entityName;
64+
}
65+
66+
/**
67+
* Get namespace.
68+
*
69+
* @return String
70+
*/
71+
public String getNamespace() {
72+
return namespace;
73+
}
74+
75+
/**
76+
* Get class FQN.
77+
*
78+
* @return String
79+
*/
80+
public String getClassFqn() {
81+
return classFqn;
82+
}
83+
84+
/**
85+
* Get entity model class FQN.
86+
*
87+
* @return String
88+
*/
89+
public String getModelClassFqn() {
90+
return modelClassFqn;
91+
}
92+
93+
/**
94+
* Get entity resource model class FQN.
95+
*
96+
* @return String
97+
*/
98+
public String getResourceModelClassFqn() {
99+
return resourceModelClassFqn;
100+
}
101+
102+
/**
103+
* Get entity DTO class FQN.
104+
*
105+
* @return String
106+
*/
107+
public String getDataModelClassFqn() {
108+
return dataModelClassFqn;
109+
}
110+
}

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

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.magento.idea.magento2plugin.actions.generation.data.PreferenceDiXmFileData;
2727
import com.magento.idea.magento2plugin.actions.generation.data.ResourceModelData;
2828
import com.magento.idea.magento2plugin.actions.generation.data.RoutesXmlData;
29+
import com.magento.idea.magento2plugin.actions.generation.data.SaveEntityCommandData;
2930
import com.magento.idea.magento2plugin.actions.generation.data.SaveEntityControllerFileData;
3031
import com.magento.idea.magento2plugin.actions.generation.data.UiComponentDataProviderData;
3132
import com.magento.idea.magento2plugin.actions.generation.data.UiComponentFormButtonData;
@@ -51,6 +52,7 @@
5152
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleResourceModelGenerator;
5253
import com.magento.idea.magento2plugin.actions.generation.generator.PreferenceDiXmlGenerator;
5354
import com.magento.idea.magento2plugin.actions.generation.generator.RoutesXmlGenerator;
55+
import com.magento.idea.magento2plugin.actions.generation.generator.SaveEntityCommandGenerator;
5456
import com.magento.idea.magento2plugin.actions.generation.generator.SaveEntityControllerFileGenerator;
5557
import com.magento.idea.magento2plugin.actions.generation.generator.UiComponentDataProviderGenerator;
5658
import com.magento.idea.magento2plugin.actions.generation.generator.UiComponentFormGenerator;
@@ -66,6 +68,7 @@
6668
import com.magento.idea.magento2plugin.magento.files.ResourceModelPhp;
6769
import com.magento.idea.magento2plugin.magento.files.UiComponentDataProviderPhp;
6870
import com.magento.idea.magento2plugin.magento.files.actions.SaveActionFile;
71+
import com.magento.idea.magento2plugin.magento.files.commands.SaveEntityCommandFile;
6972
import com.magento.idea.magento2plugin.magento.packages.Areas;
7073
import com.magento.idea.magento2plugin.magento.packages.File;
7174
import com.magento.idea.magento2plugin.magento.packages.HttpMethod;
@@ -289,6 +292,7 @@ private void onOK() {
289292
generateSaveControllerFile();
290293
generateEntityDataMapperFile();
291294
generateModelGetListQueryFile();
295+
generateSaveEntityCommandFile();
292296
generateDataProviderFile();
293297
generateLayoutFile();
294298
generateFormFile();
@@ -347,9 +351,12 @@ private void generateSaveControllerFile() {
347351
* @return String
348352
*/
349353
private String getSaveEntityCommandClassFqn() {
350-
//TODO: change this stub after the save command generated will be implemented.
351354
final NamespaceBuilder namespaceBuilder =
352-
new NamespaceBuilder(getModuleName(), "SaveCommand", "Command/" + getEntityName());
355+
new NamespaceBuilder(
356+
getModuleName(),
357+
SaveEntityCommandFile.CLASS_NAME,
358+
SaveEntityCommandFile.getDirectory(getEntityName())
359+
);
353360

354361
return namespaceBuilder.getClassFqn();
355362
}
@@ -371,14 +378,29 @@ private PsiFile generateModelFile() {
371378
), project).generate(ACTION_NAME, true);
372379
}
373380

381+
/**
382+
* Get Magento 2 model namespace builder for the entity.
383+
*
384+
* @return NamespaceBuilder
385+
*/
374386
private NamespaceBuilder getModelNamespace() {
375387
return new NamespaceBuilder(getModuleName(), getModelName(), ModelPhp.MODEL_DIRECTORY);
376388
}
377389

390+
/**
391+
* Get DTO model namespace builder for the entity.
392+
*
393+
* @return NamespaceBuilder
394+
*/
378395
private NamespaceBuilder getDataModelNamespace() {
379396
return new NamespaceBuilder(getModuleName(), getDataModelName(), DataModel.DIRECTORY);
380397
}
381398

399+
/**
400+
* Get DTO model interface namespace builder for the entity.
401+
*
402+
* @return NamespaceBuilder
403+
*/
382404
private NamespaceBuilder getDataModelInterfaceNamespace() {
383405
return new NamespaceBuilder(
384406
getModuleName(),
@@ -387,6 +409,11 @@ private NamespaceBuilder getDataModelInterfaceNamespace() {
387409
);
388410
}
389411

412+
/**
413+
* Get Magento 2 Resource model namespace builder for the entity.
414+
*
415+
* @return NamespaceBuilder
416+
*/
390417
private NamespaceBuilder getResourceModelNamespace() {
391418
return new NamespaceBuilder(
392419
getModuleName(),
@@ -1111,6 +1138,45 @@ private String getEntityDataMapperType() {
11111138
return entityDataMapperFile.getClassFqn(getModuleName());
11121139
}
11131140

1141+
/**
1142+
* Run SaveCommand.php file generator for an entity.
1143+
*/
1144+
private void generateSaveEntityCommandFile() {
1145+
final String classFqn = SaveEntityCommandFile.getClassFqn(
1146+
getModuleName(),
1147+
getEntityName()
1148+
);
1149+
final String namespace = SaveEntityCommandFile.getNamespace(
1150+
getModuleName(),
1151+
getEntityName()
1152+
);
1153+
final NamespaceBuilder modelNamespace = getModelNamespace();
1154+
final NamespaceBuilder resourceModelNamespace = getResourceModelNamespace();
1155+
final NamespaceBuilder dtoModelNamespace = getDataModelNamespace();
1156+
final NamespaceBuilder dtoInterfaceModelNamespace = getDataModelInterfaceNamespace();
1157+
1158+
final String dtoType;
1159+
1160+
if (createInterface.isSelected()) {
1161+
dtoType = dtoInterfaceModelNamespace.getClassFqn();
1162+
} else {
1163+
dtoType = dtoModelNamespace.getClassFqn();
1164+
}
1165+
1166+
new SaveEntityCommandGenerator(
1167+
new SaveEntityCommandData(
1168+
getModuleName(),
1169+
getEntityName(),
1170+
namespace,
1171+
classFqn,
1172+
modelNamespace.getClassFqn(),
1173+
resourceModelNamespace.getClassFqn(),
1174+
dtoType
1175+
),
1176+
project
1177+
).generate(ACTION_NAME, true);
1178+
}
1179+
11141180
/**
11151181
* Get tableResource field value.
11161182
*

0 commit comments

Comments
 (0)