Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@
<internalFileTemplate name="Magento Module Declarative Schema XML"/>
<internalFileTemplate name="Magento Module Declarative Schema Whitelist JSON"/>
<internalFileTemplate name="Magento Get List Query Model"/>
<internalFileTemplate name="Magento Entity Save Controller Class"/>

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
#parse("PHP File Header.php")

namespace ${NAMESPACE};

#set($uses = ${USES})
#foreach ($use in $uses.split(","))
use $use;
#end

/**
* Save ${ENTITY_NAME} controller action.
*/
class ${CLASS_NAME} extends ${EXTENDS} implements ${IMPLEMENTS}
{
/**
* Authorization level of a basic admin session
*
* @see _isAllowed()
*/
const ADMIN_RESOURCE = '${ADMIN_RESOURCE}';

/**
* @var ${DATA_PERSISTOR}
*/
private $dataPersistor;

/**
* @var ${SAVE_COMMAND}
*/
private $saveCommand;

/**
* @var ${ENTITY_DTO_FACTORY}
*/
private $entityDataFactory;

/**
* @param Context $context
* @param ${DATA_PERSISTOR} $dataPersistor
* @param ${SAVE_COMMAND} $saveCommand
* @param ${ENTITY_DTO_FACTORY} $entityDataFactory
*/
public function __construct(
Context $context,
${DATA_PERSISTOR} $dataPersistor,
${SAVE_COMMAND} $saveCommand,
${ENTITY_DTO_FACTORY} $entityDataFactory
) {
parent::__construct($context);
$this->dataPersistor = $dataPersistor;
$this->saveCommand = $saveCommand;
$this->entityDataFactory = $entityDataFactory;
}

/**
* Save ${ENTITY_NAME} Action.
*
* @return ResponseInterface|ResultInterface
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$params = $this->getRequest()->getParams();

try {
/** @var ${ENTITY_DTO}|${DATA_OBJECT} $entityModel */
$entityModel = $this->entityDataFactory->create();
$entityModel->addData($params);
$this->saveCommand->execute($entityModel);
$this->messageManager->addSuccessMessage(
__('The ${ENTITY_NAME} data was saved successfully')
);
$this->dataPersistor->clear('entity');
} catch (${COULD_NOT_SAVE} $exception) {
$this->messageManager->addErrorMessage($exception->getMessage());
$this->dataPersistor->set('entity', $params);

return $resultRedirect->setPath('*/*/edit', [
'${ENTITY_ID}'=> $this->getRequest()->getParam('${ENTITY_ID}')
]);
}

return $resultRedirect->setPath('*/*/');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<html lang="en">
<body>
<p face="verdana" size="-1">

</p>

<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
<tr>
<td colspan="3"><font face="verdana" size="-1">Template's predefined variables:</font></td>
</tr>
<tr>
<td valign="top"><nobr><font face="verdana" size="-2"><b>${NAMESPACE}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1"></font></td>
</tr>
</table>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.actions.generation.data;

import org.jetbrains.annotations.NotNull;

public class SaveEntityControllerFileData {

private final String entityName;
private final String moduleName;
private final String namespace;
private final String saveCommandFqn;
private final String dtoType;
private final String acl;
private final String entityId;

/**
* Controller save file constructor.
*
* @param entityName String
* @param moduleName String
* @param namespace String
* @param saveCommandFqn String
* @param acl String
* @param dtoType String
* @param entityId String
*/
public SaveEntityControllerFileData(
final @NotNull String entityName,
final @NotNull String moduleName,
final @NotNull String namespace,
final @NotNull String saveCommandFqn,
final @NotNull String dtoType,
final @NotNull String acl,
final @NotNull String entityId
) {
this.entityName = entityName;
this.moduleName = moduleName;
this.namespace = namespace;
this.saveCommandFqn = saveCommandFqn;
this.dtoType = dtoType;
this.acl = acl;
this.entityId = entityId;
}

/**
* Get entity id.
*
* @return String
*/
public String getEntityId() {
return entityId;
}

/**
* Get acl.
*
* @return String
*/
public String getAcl() {
return acl;
}

/**
* Get dto type.
*
* @return String
*/
public String getDtoType() {
return dtoType;
}

/**
* Get save command Fqn.
*
* @return String
*/
public String getSaveCommandFqn() {
return saveCommandFqn;
}

/**
* Get entity name.
*
* @return String
*/
public String getEntityName() {
return entityName;
}

/**
* Get module name.
*
* @return String
*/
public String getModuleName() {
return moduleName;
}

/**
* Get namespace.
*
* @return String
*/
public String getNamespace() {
return namespace;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
import com.magento.idea.magento2plugin.actions.generation.data.DataModelData;
import com.magento.idea.magento2plugin.actions.generation.data.DataModelInterfaceData;
import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlData;
import com.magento.idea.magento2plugin.actions.generation.data.GetListQueryModelData;
import com.magento.idea.magento2plugin.actions.generation.data.LayoutXmlData;
import com.magento.idea.magento2plugin.actions.generation.data.MenuXmlData;
import com.magento.idea.magento2plugin.actions.generation.data.ModelData;
import com.magento.idea.magento2plugin.actions.generation.data.GetListQueryModelData;
import com.magento.idea.magento2plugin.actions.generation.data.PreferenceDiXmFileData;
import com.magento.idea.magento2plugin.actions.generation.data.ResourceModelData;
import com.magento.idea.magento2plugin.actions.generation.data.RoutesXmlData;
import com.magento.idea.magento2plugin.actions.generation.data.SaveEntityControllerFileData;
import com.magento.idea.magento2plugin.actions.generation.data.UiComponentDataProviderData;
import com.magento.idea.magento2plugin.actions.generation.data.UiComponentFormButtonData;
import com.magento.idea.magento2plugin.actions.generation.data.UiComponentFormFieldData;
Expand All @@ -39,15 +40,16 @@
import com.magento.idea.magento2plugin.actions.generation.generator.DataModelInterfaceGenerator;
import com.magento.idea.magento2plugin.actions.generation.generator.DbSchemaWhitelistJsonGenerator;
import com.magento.idea.magento2plugin.actions.generation.generator.DbSchemaXmlGenerator;
import com.magento.idea.magento2plugin.actions.generation.generator.GetListQueryModelGenerator;
import com.magento.idea.magento2plugin.actions.generation.generator.LayoutXmlGenerator;
import com.magento.idea.magento2plugin.actions.generation.generator.MenuXmlGenerator;
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleCollectionGenerator;
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleControllerClassGenerator;
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleModelGenerator;
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleResourceModelGenerator;
import com.magento.idea.magento2plugin.actions.generation.generator.GetListQueryModelGenerator;
import com.magento.idea.magento2plugin.actions.generation.generator.PreferenceDiXmlGenerator;
import com.magento.idea.magento2plugin.actions.generation.generator.RoutesXmlGenerator;
import com.magento.idea.magento2plugin.actions.generation.generator.SaveEntityControllerFileGenerator;
import com.magento.idea.magento2plugin.actions.generation.generator.UiComponentDataProviderGenerator;
import com.magento.idea.magento2plugin.actions.generation.generator.UiComponentFormGenerator;
import com.magento.idea.magento2plugin.actions.generation.generator.UiComponentGridXmlGenerator;
Expand All @@ -60,6 +62,7 @@
import com.magento.idea.magento2plugin.magento.files.ModuleMenuXml;
import com.magento.idea.magento2plugin.magento.files.ResourceModelPhp;
import com.magento.idea.magento2plugin.magento.files.UiComponentDataProviderPhp;
import com.magento.idea.magento2plugin.magento.files.actions.SaveActionFile;
import com.magento.idea.magento2plugin.magento.packages.Areas;
import com.magento.idea.magento2plugin.magento.packages.File;
import com.magento.idea.magento2plugin.magento.packages.HttpMethod;
Expand Down Expand Up @@ -107,7 +110,8 @@
"PMD.ExcessiveImports",
"PMD.GodClass",
"PMD.TooManyMethods",
"PMD.CyclomaticComplexity"
"PMD.CyclomaticComplexity",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @eduard13

Please consider that this part will be refactored in the scope of the separate task.

Copy link
Contributor

@VitaliyBoyko VitaliyBoyko Feb 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the extended testing will be performed in the scope of the whole mainline.

"PMD.ExcessiveClassLength"
})
public class NewEntityDialog extends AbstractDialog {
@NotNull
Expand Down Expand Up @@ -279,7 +283,7 @@ private void onOK() {

generateRoutesXmlFile();
generateViewControllerFile();
generateSubmitControllerFile();
generateSaveControllerFile();
generateModelGetListQueryFile();
generateDataProviderFile();
generateLayoutFile();
Expand All @@ -303,6 +307,49 @@ private void onOK() {
this.setVisible(false);
}

/**
* Generate Save Controller file.
*/
private void generateSaveControllerFile() {
final NamespaceBuilder dtoModelNamespace = getDataModelNamespace();
final NamespaceBuilder dtoInterfaceModelNamespace = getDataModelInterfaceNamespace();
final NamespaceBuilder namespace = new NamespaceBuilder(
getModuleName(),
SaveActionFile.CLASS_NAME,
SaveActionFile.getDirectory(getEntityName())
);
final String dtoType;

if (createInterface.isSelected()) {
dtoType = dtoInterfaceModelNamespace.getClassFqn();
} else {
dtoType = dtoModelNamespace.getClassFqn();
}

new SaveEntityControllerFileGenerator(new SaveEntityControllerFileData(
getEntityName(),
getModuleName(),
namespace.getNamespace(),
getSaveEntityCommandClassFqn(),
dtoType,
getAcl(),
getEntityIdColumn()
), project).generate(ACTION_NAME, false);
}

/**
* Get save entity command class Fqn.
*
* @return String
*/
private String getSaveEntityCommandClassFqn() {
//TODO: change this stub after the save command generated will be implemented.
final NamespaceBuilder namespaceBuilder =
new NamespaceBuilder(getModuleName(), "SaveCommand", "Command/" + getEntityName());

return namespaceBuilder.getClassFqn();
}

private PsiFile generateModelFile() {
final NamespaceBuilder modelNamespace = getModelNamespace();
final NamespaceBuilder resourceModelNamespace = getResourceModelNamespace();
Expand Down Expand Up @@ -574,24 +621,11 @@ private String getControllerDirectory() {
return ControllerBackendPhp.DEFAULT_DIR + File.separator;
}

private PsiFile generateSubmitControllerFile() {
final NamespaceBuilder namespace = new NamespaceBuilder(
getModuleName(),
getSubmitActionName(),
getViewControllerDirectory()
);
return new ModuleControllerClassGenerator(new ControllerFileData(
getViewControllerDirectory(),
getSubmitActionName(),
getModuleName(),
Areas.adminhtml.toString(),
HttpMethod.POST.toString(),
getAcl(),
true,
namespace.getNamespace()
), project).generate(ACTION_NAME, false);
}

/**
* Get Acl id.
*
* @return String
*/
public String getAcl() {
return acl.getText().trim();
}
Expand Down
Loading