Skip to content

Commit 9c4be7f

Browse files
committed
Added Generator for Delete Controller
1 parent ca7f84c commit 9c4be7f

File tree

10 files changed

+500
-1
lines changed

10 files changed

+500
-1
lines changed

resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@
238238
<internalFileTemplate name="Magento PHP Form Generic Button Block Class"/>
239239
<internalFileTemplate name="Magento Entity New Action Controller Class"/>
240240
<internalFileTemplate name="Magento New Entity Layout XML"/>
241+
<internalFileTemplate name="Magento Entity Delete Controller Class"/>
241242

242243
<defaultLiveTemplates file="/liveTemplates/MagentoPWA.xml"/>
243244

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
@@ -20,6 +20,7 @@
2020
import com.magento.idea.magento2plugin.actions.generation.data.DataModelData;
2121
import com.magento.idea.magento2plugin.actions.generation.data.DataModelInterfaceData;
2222
import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlData;
23+
import com.magento.idea.magento2plugin.actions.generation.data.DeleteEntityControllerFileData;
2324
import com.magento.idea.magento2plugin.actions.generation.data.EntityDataMapperData;
2425
import com.magento.idea.magento2plugin.actions.generation.data.FormGenericButtonBlockData;
2526
import com.magento.idea.magento2plugin.actions.generation.data.GetListQueryModelData;
@@ -56,6 +57,7 @@
5657
import com.magento.idea.magento2plugin.actions.generation.generator.DataModelInterfaceGenerator;
5758
import com.magento.idea.magento2plugin.actions.generation.generator.DbSchemaWhitelistJsonGenerator;
5859
import com.magento.idea.magento2plugin.actions.generation.generator.DbSchemaXmlGenerator;
60+
import com.magento.idea.magento2plugin.actions.generation.generator.DeleteEntityControllerFileGenerator;
5961
import com.magento.idea.magento2plugin.actions.generation.generator.EntityDataMapperGenerator;
6062
import com.magento.idea.magento2plugin.actions.generation.generator.FormGenericButtonBlockGenerator;
6163
import com.magento.idea.magento2plugin.actions.generation.generator.GetListQueryModelGenerator;
@@ -373,6 +375,7 @@ private void onOK() {
373375
generateFormSaveControllerFile();
374376
generateFormUiComponentGenericButtonFile();
375377
generateFormNewActionControllerFile();
378+
generateFormDeleteControllerFile();
376379
generateUiComponentFormFile();
377380
}
378381

@@ -1251,6 +1254,43 @@ private void generateFormNewActionControllerFile() {
12511254
), project).generate(ACTION_NAME, false);
12521255
}
12531256

1257+
/**
1258+
* Generate Delete Controller file.
1259+
*/
1260+
private void generateFormDeleteControllerFile() {
1261+
final NamespaceBuilder namespace = new NamespaceBuilder(
1262+
getModuleName(),
1263+
NewActionFile.CLASS_NAME,
1264+
NewActionFile.getDirectory(getEntityName())
1265+
);
1266+
1267+
new DeleteEntityControllerFileGenerator(new DeleteEntityControllerFileData(
1268+
getEntityName(),
1269+
getModuleName(),
1270+
namespace.getNamespace(),
1271+
getDeleteEntityCommandClassFqn(),
1272+
getAcl(),
1273+
getEntityIdColumn()
1274+
), project).generate(ACTION_NAME, false);
1275+
}
1276+
1277+
/**
1278+
* Get delete entity command class Fqn.
1279+
*
1280+
* @return String
1281+
*/
1282+
private String getDeleteEntityCommandClassFqn() {
1283+
final NamespaceBuilder namespaceBuilder =
1284+
new NamespaceBuilder(
1285+
getModuleName(),
1286+
"DeleteByIdCommand", //TODO after command added: DeleteEntityCommandFile.CLASS_NAME,
1287+
"Command".concat("/" + getEntityName()) //TODO after command added: DeleteEntityCommandFile.getDirectory(getEntityName())
1288+
);
1289+
1290+
return namespaceBuilder.getClassFqn();
1291+
}
1292+
1293+
12541294
/**
12551295
* Get save entity command class Fqn.
12561296
*
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)