Skip to content

Commit 42f855b

Browse files
Added save entity command class generation
1 parent 5a68b4c commit 42f855b

File tree

12 files changed

+722
-0
lines changed

12 files changed

+722
-0
lines changed

resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@
229229
<internalFileTemplate name="Magento Module Declarative Schema XML"/>
230230
<internalFileTemplate name="Magento Module Declarative Schema Whitelist JSON"/>
231231
<internalFileTemplate name="Magento Get List Query Model"/>
232+
<internalFileTemplate name="Magento Save Entity Command Model"/>
232233

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

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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.magento.idea.magento2plugin.actions.generation.data.PreferenceDiXmFileData;
2626
import com.magento.idea.magento2plugin.actions.generation.data.ResourceModelData;
2727
import com.magento.idea.magento2plugin.actions.generation.data.RoutesXmlData;
28+
import com.magento.idea.magento2plugin.actions.generation.data.SaveEntityCommandData;
2829
import com.magento.idea.magento2plugin.actions.generation.data.UiComponentDataProviderData;
2930
import com.magento.idea.magento2plugin.actions.generation.data.UiComponentFormButtonData;
3031
import com.magento.idea.magento2plugin.actions.generation.data.UiComponentFormFieldData;
@@ -48,6 +49,7 @@
4849
import com.magento.idea.magento2plugin.actions.generation.generator.GetListQueryModelGenerator;
4950
import com.magento.idea.magento2plugin.actions.generation.generator.PreferenceDiXmlGenerator;
5051
import com.magento.idea.magento2plugin.actions.generation.generator.RoutesXmlGenerator;
52+
import com.magento.idea.magento2plugin.actions.generation.generator.SaveEntityCommandGenerator;
5153
import com.magento.idea.magento2plugin.actions.generation.generator.UiComponentDataProviderGenerator;
5254
import com.magento.idea.magento2plugin.actions.generation.generator.UiComponentFormGenerator;
5355
import com.magento.idea.magento2plugin.actions.generation.generator.UiComponentGridXmlGenerator;
@@ -60,6 +62,7 @@
6062
import com.magento.idea.magento2plugin.magento.files.ModuleMenuXml;
6163
import com.magento.idea.magento2plugin.magento.files.ResourceModelPhp;
6264
import com.magento.idea.magento2plugin.magento.files.UiComponentDataProviderPhp;
65+
import com.magento.idea.magento2plugin.magento.files.commands.SaveEntityCommandFile;
6366
import com.magento.idea.magento2plugin.magento.packages.Areas;
6467
import com.magento.idea.magento2plugin.magento.packages.File;
6568
import com.magento.idea.magento2plugin.magento.packages.HttpMethod;
@@ -281,6 +284,7 @@ private void onOK() {
281284
generateViewControllerFile();
282285
generateSubmitControllerFile();
283286
generateModelGetListQueryFile();
287+
generateSaveEntityCommandFile();
284288
generateDataProviderFile();
285289
generateLayoutFile();
286290
generateFormFile();
@@ -320,14 +324,29 @@ private PsiFile generateModelFile() {
320324
), project).generate(ACTION_NAME, true);
321325
}
322326

327+
/**
328+
* Get Magento 2 model namespace builder for the entity.
329+
*
330+
* @return NamespaceBuilder
331+
*/
323332
private NamespaceBuilder getModelNamespace() {
324333
return new NamespaceBuilder(getModuleName(), getModelName(), ModelPhp.MODEL_DIRECTORY);
325334
}
326335

336+
/**
337+
* Get DTO model namespace builder for the entity.
338+
*
339+
* @return NamespaceBuilder
340+
*/
327341
private NamespaceBuilder getDataModelNamespace() {
328342
return new NamespaceBuilder(getModuleName(), getDataModelName(), DataModel.DIRECTORY);
329343
}
330344

345+
/**
346+
* Get DTO model interface namespace builder for the entity.
347+
*
348+
* @return NamespaceBuilder
349+
*/
331350
private NamespaceBuilder getDataModelInterfaceNamespace() {
332351
return new NamespaceBuilder(
333352
getModuleName(),
@@ -336,6 +355,11 @@ private NamespaceBuilder getDataModelInterfaceNamespace() {
336355
);
337356
}
338357

358+
/**
359+
* Get Magento 2 Resource model namespace builder for the entity.
360+
*
361+
* @return NamespaceBuilder
362+
*/
339363
private NamespaceBuilder getResourceModelNamespace() {
340364
return new NamespaceBuilder(
341365
getModuleName(),
@@ -1036,6 +1060,45 @@ private String getEntityDataMapperType() {
10361060
return "Test\\Test\\Mapper\\" + getEntityName() + "DataMapper";
10371061
}
10381062

1063+
/**
1064+
* Run SaveCommand.php file generator for an entity.
1065+
*/
1066+
private void generateSaveEntityCommandFile() {
1067+
final String classFqn = SaveEntityCommandFile.getClassFqn(
1068+
getModuleName(),
1069+
getEntityName()
1070+
);
1071+
final String namespace = SaveEntityCommandFile.getNamespace(
1072+
getModuleName(),
1073+
getEntityName()
1074+
);
1075+
final NamespaceBuilder modelNamespace = getModelNamespace();
1076+
final NamespaceBuilder resourceModelNamespace = getResourceModelNamespace();
1077+
final NamespaceBuilder dtoModelNamespace = getDataModelNamespace();
1078+
final NamespaceBuilder dtoInterfaceModelNamespace = getDataModelInterfaceNamespace();
1079+
1080+
final String dtoType;
1081+
1082+
if (createInterface.isSelected()) {
1083+
dtoType = dtoInterfaceModelNamespace.getClassFqn();
1084+
} else {
1085+
dtoType = dtoModelNamespace.getClassFqn();
1086+
}
1087+
1088+
new SaveEntityCommandGenerator(
1089+
new SaveEntityCommandData(
1090+
getModuleName(),
1091+
getEntityName(),
1092+
namespace,
1093+
classFqn,
1094+
modelNamespace.getClassFqn(),
1095+
resourceModelNamespace.getClassFqn(),
1096+
dtoType
1097+
),
1098+
project
1099+
).generate(ACTION_NAME, true);
1100+
}
1101+
10391102
/**
10401103
* Get tableResource field value.
10411104
*

0 commit comments

Comments
 (0)