Skip to content

Entity web api generation #607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ class ${CLASS_NAME}
* @param int $entityId
*
* @return void
* @throws ${COULD_NOT_DELETE}|${NO_SUCH_ENTITY_EXCEPTION}
* @throws ${COULD_NOT_DELETE}
*/
public function execute(int $entityId)
public function execute(int $entityId): void
{
try {
/** @var ${MODEL} $model */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
#parse("PHP File Header.php")

namespace ${NAMESPACE};

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

/**
* ${ENTITY_NAME} entity search result.
*/
interface ${CLASS_NAME} extends ${PARENT_INTERFACE}
{
/**
* Set items.
*
* @param ${DTO_TYPE}[] $items
*
* @return ${CLASS_NAME}
*/
public function setItems(array $items): ${CLASS_NAME};

/**
* Get items.
*
* @return ${DTO_TYPE}[]
*/
public function getItems(): array;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
#parse("PHP File Header.php")

namespace ${NAMESPACE};

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

/**
* ${ENTITY_NAME} entity search results implementation.
*/
class ${CLASS_NAME} extends ${PARENT_CLASS_NAME} implements ${INTERFACE_NAME}
{
/**
* @inheritDoc
*/
public function setItems(array $items): ${INTERFACE_NAME}
{
return parent::setItems($items);
}

/**
* @inheritDoc
*/
public function getItems(): array
{
return parent::getItems();
}
}
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ${CLASS_NAME}
/**
* Save ${ENTITY_NAME}.
*
* @param ${DTO}|${DATA_OBJECT} $${DTO_PROPERTY}
* @param ${DTO} $${DTO_PROPERTY}
*
* @return int
* @throws ${COULD_NOT_SAVE}
Expand Down Expand Up @@ -74,6 +74,6 @@ class ${CLASS_NAME}
throw new ${COULD_NOT_SAVE}(__('Could not save ${ENTITY_NAME}.'));
}

return (int) $model->getEntityId();
return (int) $model->getData(${ENTITY_ID_CONST});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class GetListQueryModelData {
private final String modelName;
private final String collectionName;
private final String aclResource;
private final boolean hasWebApi;

/**
* Query Model DTO Constructor.
Expand All @@ -23,19 +24,22 @@ public class GetListQueryModelData {
* @param modelName String
* @param collectionName String
* @param aclResource String
* @param hasWebApi boolean
*/
public GetListQueryModelData(
final @NotNull String moduleName,
final @NotNull String entityName,
final @NotNull String modelName,
final @NotNull String collectionName,
final @NotNull String aclResource
final @NotNull String aclResource,
final boolean hasWebApi
) {
this.moduleName = moduleName;
this.entityName = entityName;
this.modelName = modelName;
this.collectionName = collectionName;
this.aclResource = aclResource;
this.hasWebApi = hasWebApi;
}

/**
Expand Down Expand Up @@ -82,4 +86,13 @@ public String getCollectionName() {
public String getAclResource() {
return aclResource;
}

/**
* Check if entity has Web API.
*
* @return boolean
*/
public boolean isHasWebApi() {
return hasWebApi;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public GetListQueryDtoConverter(
newEntityDialogData.getEntityName(),
newEntityDialogData.getEntityName().concat("Model"),
newEntityDialogData.getEntityName().concat("Collection"),
newEntityDialogData.getAclId()
newEntityDialogData.getAclId(),
newEntityDialogData.hasWebApi()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

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

import com.magento.idea.magento2plugin.actions.generation.data.converter.DataObjectConverter;
import com.magento.idea.magento2plugin.actions.generation.data.dialog.EntityCreatorContextData;
import com.magento.idea.magento2plugin.actions.generation.data.dialog.NewEntityDialogData;
import com.magento.idea.magento2plugin.actions.generation.data.php.SearchResultsData;
import org.jetbrains.annotations.NotNull;

public class SearchResultsDtoConverter extends SearchResultsData implements DataObjectConverter {

/**
* Save entity command DTO converter.
*
* @param generationContextData EntityCreatorContextData
* @param newEntityDialogData NewEntityDialogData
*/
public SearchResultsDtoConverter(
final @NotNull EntityCreatorContextData generationContextData,
final @NotNull NewEntityDialogData newEntityDialogData
) {
super(
generationContextData.getModuleName(),
newEntityDialogData.getEntityName(),
newEntityDialogData.hasDtoInterface()
? generationContextData.getDtoInterfaceNamespaceBuilder().getClassFqn()
: generationContextData.getDtoModelNamespaceBuilder().getClassFqn()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

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

import org.jetbrains.annotations.NotNull;

public class SearchResultsData {

private final String moduleName;
private final String entityName;
private final String dtoType;

/**
* Entity search results data transfer object constructor.
*
* @param moduleName String
* @param entityName String
* @param dtoType String
*/
public SearchResultsData(
final @NotNull String moduleName,
final @NotNull String entityName,
final @NotNull String dtoType
) {
this.moduleName = moduleName;
this.entityName = entityName;
this.dtoType = dtoType;
}

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

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

/**
* Get entity DTO type.
*
* @return String
*/
public String getDtoType() {
return dtoType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ public class NewEntityDialog extends AbstractDialog {
private static final String DTO_INTERFACE_SUFFIX = "Interface";
private static final String DATA_PROVIDER_SUFFIX = "DataProvider";

private static final String DEFAULT_MENU_SORT_ORDER = "100";

private static final boolean OPEN_FILES_FLAG = false;

@FieldValidation(rule = RuleRegistry.NOT_EMPTY, message = {NotEmptyRule.MESSAGE, ENTITY_ID})
Expand Down Expand Up @@ -291,6 +293,8 @@ protected void textChanged(final @NotNull DocumentEvent event) {

createUiComponent.addItemListener(event -> toggleUiComponentsPanel());
registerTabbedPane(tabbedPane1);

sortOrder.setText(DEFAULT_MENU_SORT_ORDER);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile;
import com.magento.idea.magento2plugin.magento.files.CollectionModelFile;
import com.magento.idea.magento2plugin.magento.files.EntityDataMapperFile;
import com.magento.idea.magento2plugin.magento.files.SearchResultsInterfaceFile;
import com.magento.idea.magento2plugin.magento.files.queries.GetListQueryFile;
import com.magento.idea.magento2plugin.magento.packages.code.FrameworkLibraryType;
import java.util.Properties;
Expand Down Expand Up @@ -100,14 +101,24 @@ protected void fillAttributes(final @NotNull Properties attributes) {
.append(
"SEARCH_CRITERIA_TYPE",
FrameworkLibraryType.SEARCH_CRITERIA.getType()
)
.append(
"SEARCH_RESULT_TYPE",
FrameworkLibraryType.SEARCH_RESULT.getType()
)
.append(
"SEARCH_RESULT_FACTORY_TYPE",
FrameworkLibraryType.SEARCH_RESULT.getFactory()
);

String searchResultType;
String searchResultFactoryType;

if (data.isHasWebApi()) {
final SearchResultsInterfaceFile searchResultsInterfaceFile =
new SearchResultsInterfaceFile(data.getModuleName(), data.getEntityName());

searchResultType = searchResultsInterfaceFile.getClassFqn();
searchResultFactoryType = searchResultType.concat("Factory");
} else {
searchResultType = FrameworkLibraryType.SEARCH_RESULT.getType();
searchResultFactoryType = FrameworkLibraryType.SEARCH_RESULT.getFactory();
}

typesBuilder
.append("SEARCH_RESULT_TYPE", searchResultType)
.append("SEARCH_RESULT_FACTORY_TYPE", searchResultFactoryType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ protected void fillAttributes(final @NotNull Properties attributes) {
.append("ENTITY_NAME", data.getEntityName(), false)
.append("CLASS_NAME", SaveEntityCommandFile.CLASS_NAME, false)
.append("EXCEPTION", "Exception")
.append("DATA_OBJECT", FrameworkLibraryType.DATA_OBJECT.getType())
.append("COULD_NOT_SAVE", ExceptionType.COULD_NOT_SAVE.getType())
.append("LOGGER", FrameworkLibraryType.LOGGER.getType());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.actions.generation.generator.php;

import com.intellij.openapi.project.Project;
import com.magento.idea.magento2plugin.actions.generation.data.php.SearchResultsData;
import com.magento.idea.magento2plugin.actions.generation.generator.PhpFileGenerator;
import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile;
import com.magento.idea.magento2plugin.magento.files.SearchResultsFile;
import com.magento.idea.magento2plugin.magento.files.SearchResultsInterfaceFile;
import com.magento.idea.magento2plugin.magento.packages.code.FrameworkLibraryType;
import java.util.Properties;
import org.jetbrains.annotations.NotNull;

public class SearchResultsGenerator extends PhpFileGenerator {

private final SearchResultsData data;

/**
* Search results file generator constructor.
*
* @param data SearchResultsData
* @param project Project
*/
public SearchResultsGenerator(
final @NotNull SearchResultsData data,
final @NotNull Project project
) {
this(data, project, true);
}

/**
* Search results file generator constructor.
*
* @param data SearchResultsData
* @param project Project
* @param checkFileAlreadyExists boolean
*/
public SearchResultsGenerator(
final @NotNull SearchResultsData data,
final @NotNull Project project,
final boolean checkFileAlreadyExists
) {
super(project, checkFileAlreadyExists);
this.data = data;
}

@Override
protected AbstractPhpFile initFile() {
return new SearchResultsFile(data.getModuleName(), data.getEntityName());
}

@Override
protected void fillAttributes(final @NotNull Properties attributes) {
final String interfaceClassFqn = new SearchResultsInterfaceFile(
data.getModuleName(),
data.getEntityName()
).getClassFqn();

typesBuilder
.append("NAMESPACE", file.getNamespace(), false)
.append("ENTITY_NAME", data.getEntityName(), false)
.append("CLASS_NAME", file.getClassName(), false)
.append("INTERFACE_NAME", interfaceClassFqn)
.append(
"PARENT_CLASS_NAME",
FrameworkLibraryType.SEARCH_RESULT_IMPLEMENTATION.getType()
);
}
}
Loading