Skip to content

Commit 0b0e6ea

Browse files
author
Vitaliy
authored
Merge pull request #493 from anzin/add-generator-for-delete-controller
Added Generator for Delete Controller
2 parents 572aa55 + e92ab1d commit 0b0e6ea

File tree

9 files changed

+497
-0
lines changed

9 files changed

+497
-0
lines changed

resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@
240240
<internalFileTemplate name="Magento New Entity Layout XML"/>
241241
<internalFileTemplate name="Magento Delete Entity By Id Command"/>
242242
<internalFileTemplate name="Magento Entity Edit Action Controller Class"/>
243+
<internalFileTemplate name="Magento Entity Delete Controller Class"/>
243244

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
* Delete ${ENTITY_NAME} controller.
13+
*/
14+
class ${CLASS_NAME} extends ${EXTENDS} implements ${IMPLEMENTS_POST}, ${IMPLEMENTS_GET}
15+
{
16+
/**
17+
* Authorization level of a basic admin session.
18+
*
19+
* @see _isAllowed()
20+
*/
21+
const ADMIN_RESOURCE = '${ADMIN_RESOURCE}';
22+
23+
/**
24+
* @var ${DELETE_COMMAND}
25+
*/
26+
private $deleteByIdCommand;
27+
28+
/**
29+
* @param Context $context
30+
* @param DeleteByIdCommand $deleteByIdCommand
31+
*/
32+
public function __construct(
33+
${CONTEXT} $context,
34+
${DELETE_COMMAND} $deleteByIdCommand
35+
) {
36+
parent::__construct($context);
37+
$this->deleteByIdCommand = $deleteByIdCommand;
38+
}
39+
40+
/**
41+
* Delete ${ENTITY_NAME} action.
42+
*
43+
* @return ${RESULT_INTERFACE}
44+
*/
45+
public function execute()
46+
{
47+
/** @var ${RESULT_INTERFACE} $resultRedirect */
48+
$resultRedirect = $this->resultFactory->create(${RESULT_FACTORY}::TYPE_REDIRECT);
49+
$resultRedirect->setPath('*/*/');
50+
$entityId = (int) $this->getRequest()->getParam('${ENTITY_ID}');
51+
52+
try {
53+
$this->deleteByIdCommand->execute($entityId);
54+
$this->messageManager->addSuccessMessage(__('You have successfully deleted ${ENTITY_NAME} entity'));
55+
} catch (${COULD_NOT_DELETE} | ${NO_SUCH_ENTITY_EXCEPTION} $exception) {
56+
$this->messageManager->addErrorMessage($exception->getMessage());
57+
}
58+
59+
return $resultRedirect;
60+
}
61+
}
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: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 DeleteEntityControllerFileData {
11+
12+
private final String entityName;
13+
private final String moduleName;
14+
private final String namespace;
15+
private final String deleteCommandFqn;
16+
private final String acl;
17+
private final String entityId;
18+
19+
/**
20+
* Controller Delete file constructor.
21+
*
22+
* @param entityName String
23+
* @param moduleName String
24+
* @param namespace String
25+
* @param acl String
26+
* @param entityId String
27+
*/
28+
public DeleteEntityControllerFileData(
29+
final @NotNull String entityName,
30+
final @NotNull String moduleName,
31+
final @NotNull String namespace,
32+
final @NotNull String deleteCommandFqn,
33+
final @NotNull String acl,
34+
final @NotNull String entityId
35+
) {
36+
this.entityName = entityName;
37+
this.moduleName = moduleName;
38+
this.namespace = namespace;
39+
this.deleteCommandFqn = deleteCommandFqn;
40+
this.acl = acl;
41+
this.entityId = entityId;
42+
}
43+
44+
/**
45+
* Get entity name.
46+
*
47+
* @return String
48+
*/
49+
public String getEntityName() {
50+
return entityName;
51+
}
52+
53+
/**
54+
* Get module name.
55+
*
56+
* @return String
57+
*/
58+
public String getModuleName() {
59+
return moduleName;
60+
}
61+
62+
/**
63+
* Get namespace.
64+
*
65+
* @return String
66+
*/
67+
public String getNamespace() {
68+
return namespace;
69+
}
70+
71+
/**
72+
* Get delete command Fqn.
73+
*
74+
* @return String
75+
*/
76+
public String getDeleteCommandFqn() {
77+
return deleteCommandFqn;
78+
}
79+
80+
/**
81+
* Get acl.
82+
*
83+
* @return String
84+
*/
85+
public String getAcl() {
86+
return acl;
87+
}
88+
89+
/**
90+
* Get entity Id.
91+
*
92+
* @return String
93+
*/
94+
public String getEntityId() {
95+
return entityId;
96+
}
97+
}

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlData;
2222
import com.magento.idea.magento2plugin.actions.generation.data.EditEntityActionData;
2323
import com.magento.idea.magento2plugin.actions.generation.data.DeleteEntityByIdCommandData;
24+
import com.magento.idea.magento2plugin.actions.generation.data.DeleteEntityControllerFileData;
2425
import com.magento.idea.magento2plugin.actions.generation.data.EntityDataMapperData;
2526
import com.magento.idea.magento2plugin.actions.generation.data.FormGenericButtonBlockData;
2627
import com.magento.idea.magento2plugin.actions.generation.data.GetListQueryModelData;
@@ -59,6 +60,7 @@
5960
import com.magento.idea.magento2plugin.actions.generation.generator.DbSchemaXmlGenerator;
6061
import com.magento.idea.magento2plugin.actions.generation.generator.EditEntityActionGenerator;
6162
import com.magento.idea.magento2plugin.actions.generation.generator.DeleteEntityByIdCommandGenerator;
63+
import com.magento.idea.magento2plugin.actions.generation.generator.DeleteEntityControllerFileGenerator;
6264
import com.magento.idea.magento2plugin.actions.generation.generator.EntityDataMapperGenerator;
6365
import com.magento.idea.magento2plugin.actions.generation.generator.FormGenericButtonBlockGenerator;
6466
import com.magento.idea.magento2plugin.actions.generation.generator.GetListQueryModelGenerator;
@@ -378,6 +380,7 @@ private void onOK() {
378380
generateFormEditControllerFile();
379381
generateFormSaveControllerFile();
380382
generateFormNewActionControllerFile();
383+
generateFormDeleteControllerFile();
381384
generateUiComponentFormFile();
382385
}
383386

@@ -1293,6 +1296,43 @@ private void generateFormNewActionControllerFile() {
12931296
), project).generate(ACTION_NAME, false);
12941297
}
12951298

1299+
/**
1300+
* Generate Delete Controller file.
1301+
*/
1302+
private void generateFormDeleteControllerFile() {
1303+
final NamespaceBuilder namespace = new NamespaceBuilder(
1304+
getModuleName(),
1305+
NewActionFile.CLASS_NAME,
1306+
NewActionFile.getDirectory(getEntityName())
1307+
);
1308+
1309+
new DeleteEntityControllerFileGenerator(new DeleteEntityControllerFileData(
1310+
getEntityName(),
1311+
getModuleName(),
1312+
namespace.getNamespace(),
1313+
getDeleteEntityCommandClassFqn(),
1314+
getAcl(),
1315+
getEntityIdColumn()
1316+
), project).generate(ACTION_NAME, false);
1317+
}
1318+
1319+
/**
1320+
* Get delete entity command class Fqn.
1321+
*
1322+
* @return String
1323+
*/
1324+
private String getDeleteEntityCommandClassFqn() {
1325+
final NamespaceBuilder namespaceBuilder =
1326+
new NamespaceBuilder(
1327+
getModuleName(),
1328+
DeleteEntityByIdCommandFile.CLASS_NAME,
1329+
DeleteEntityByIdCommandFile.getDirectory(getEntityName())
1330+
);
1331+
1332+
return namespaceBuilder.getClassFqn();
1333+
}
1334+
1335+
12961336
/**
12971337
* Get save entity command class Fqn.
12981338
*
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.magento.idea.magento2plugin.actions.generation.generator;
2+
3+
import com.intellij.openapi.project.Project;
4+
import com.intellij.psi.PsiDirectory;
5+
import com.intellij.psi.PsiFile;
6+
import com.magento.idea.magento2plugin.actions.generation.data.DeleteEntityControllerFileData;
7+
import com.magento.idea.magento2plugin.actions.generation.generator.util.DirectoryGenerator;
8+
import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator;
9+
import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil;
10+
import com.magento.idea.magento2plugin.indexes.ModuleIndex;
11+
import com.magento.idea.magento2plugin.magento.files.actions.DeleteActionFile;
12+
import com.magento.idea.magento2plugin.magento.packages.HttpMethod;
13+
import com.magento.idea.magento2plugin.magento.packages.code.BackendModuleType;
14+
import com.magento.idea.magento2plugin.magento.packages.code.ExceptionType;
15+
import com.magento.idea.magento2plugin.magento.packages.code.FrameworkLibraryType;
16+
import java.util.ArrayList;
17+
import java.util.List;
18+
import java.util.Properties;
19+
import org.jetbrains.annotations.NotNull;
20+
21+
public class DeleteEntityControllerFileGenerator extends FileGenerator {
22+
23+
private final DeleteEntityControllerFileData fileData;
24+
private final FileFromTemplateGenerator fileFromTemplateGenerator;
25+
private final DirectoryGenerator directoryGenerator;
26+
private final ModuleIndex moduleIndex;
27+
private final List<String> uses;
28+
29+
/**
30+
* Delete Entity Controller File Generator.
31+
* @param fileData DeleteEntityControllerFileData
32+
* @param project Project
33+
*/
34+
public DeleteEntityControllerFileGenerator(
35+
final DeleteEntityControllerFileData fileData,
36+
final @NotNull Project project
37+
) {
38+
super(project);
39+
this.fileData = fileData;
40+
fileFromTemplateGenerator = FileFromTemplateGenerator.getInstance(project);
41+
moduleIndex = ModuleIndex.getInstance(project);
42+
directoryGenerator = DirectoryGenerator.getInstance();
43+
uses = new ArrayList<>();
44+
}
45+
46+
/**
47+
* Generate Delete controller.
48+
*
49+
* @param actionName String
50+
*
51+
* @return PsiFile
52+
*/
53+
@Override
54+
public PsiFile generate(final @NotNull String actionName) {
55+
final PsiDirectory moduleBaseDir = moduleIndex.getModuleDirectoryByModuleName(
56+
fileData.getModuleName()
57+
);
58+
final PsiDirectory baseDirectory = directoryGenerator.findOrCreateSubdirectories(
59+
moduleBaseDir,
60+
DeleteActionFile.getDirectory(fileData.getEntityName())
61+
);
62+
63+
return fileFromTemplateGenerator.generate(
64+
DeleteActionFile.getInstance(),
65+
getAttributes(),
66+
baseDirectory,
67+
actionName
68+
);
69+
}
70+
71+
@Override
72+
protected void fillAttributes(final @NotNull Properties attributes) {
73+
attributes.setProperty("NAMESPACE", fileData.getNamespace());
74+
attributes.setProperty("ENTITY_NAME", fileData.getEntityName());
75+
attributes.setProperty("CLASS_NAME", DeleteActionFile.CLASS_NAME);
76+
attributes.setProperty("ADMIN_RESOURCE", fileData.getAcl());
77+
attributes.setProperty("ENTITY_ID", fileData.getEntityId());
78+
addProperty(attributes, "DELETE_COMMAND", fileData.getDeleteCommandFqn());
79+
addProperty(attributes, "CONTEXT", BackendModuleType.CONTEXT.getType());
80+
addProperty(attributes, "RESULT_FACTORY", FrameworkLibraryType.RESULT_FACTORY.getType());
81+
addProperty(attributes, "RESULT_INTERFACE", FrameworkLibraryType.RESULT_INTERFACE.getType());
82+
addProperty(attributes, "EXTENDS", BackendModuleType.EXTENDS.getType());
83+
addProperty(attributes, "IMPLEMENTS_POST", HttpMethod.POST.getInterfaceFqn());
84+
addProperty(attributes, "IMPLEMENTS_GET", HttpMethod.GET.getInterfaceFqn());
85+
addProperty(attributes, "NO_SUCH_ENTITY_EXCEPTION",
86+
ExceptionType.NO_SUCH_ENTITY_EXCEPTION.getType());
87+
addProperty(attributes, "COULD_NOT_DELETE",
88+
ExceptionType.COULD_NOT_DELETE.getType());
89+
attributes.setProperty("USES", PhpClassGeneratorUtil.formatUses(uses));
90+
}
91+
92+
/**
93+
* Add type to properties.
94+
*
95+
* @param properties Properties
96+
* @param propertyName String
97+
* @param type String
98+
*/
99+
protected void addProperty(
100+
final @NotNull Properties properties,
101+
final String propertyName,
102+
final String type
103+
) {
104+
uses.add(type);
105+
properties.setProperty(propertyName, PhpClassGeneratorUtil.getNameFromFqn(type));
106+
}
107+
}

0 commit comments

Comments
 (0)