|
| 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.generator; |
| 7 | + |
| 8 | +import com.intellij.json.psi.JsonElement; |
| 9 | +import com.intellij.json.psi.JsonElementGenerator; |
| 10 | +import com.intellij.json.psi.JsonFile; |
| 11 | +import com.intellij.json.psi.JsonObject; |
| 12 | +import com.intellij.json.psi.JsonProperty; |
| 13 | +import com.intellij.json.psi.JsonPsiUtil; |
| 14 | +import com.intellij.json.psi.impl.JsonObjectImpl; |
| 15 | +import com.intellij.openapi.command.WriteCommandAction; |
| 16 | +import com.intellij.openapi.editor.Document; |
| 17 | +import com.intellij.openapi.project.Project; |
| 18 | +import com.intellij.psi.PsiDocumentManager; |
| 19 | +import com.intellij.psi.PsiFile; |
| 20 | +import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlData; |
| 21 | +import com.magento.idea.magento2plugin.actions.generation.generator.util.FindOrCreateDbSchemaWhitelistJson; |
| 22 | +import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaWhitelistJson; |
| 23 | +import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaXml; |
| 24 | +import java.util.Arrays; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Properties; |
| 27 | +import org.jetbrains.annotations.NotNull; |
| 28 | + |
| 29 | +/** |
| 30 | + * The db_schema_whitelist.json file generator. |
| 31 | + */ |
| 32 | +public class DbSchemaWhitelistJsonGenerator extends FileGenerator { |
| 33 | + |
| 34 | + private final Project project; |
| 35 | + private final JsonElementGenerator jsonElementGenerator; |
| 36 | + private final DbSchemaXmlData dbSchemaXmlData; |
| 37 | + private final String moduleName; |
| 38 | + |
| 39 | + /** |
| 40 | + * The db_schema_whitelist.json file generator constructor. |
| 41 | + * |
| 42 | + * @param project Project |
| 43 | + * @param dbSchemaXmlData DbSchemaXmlData |
| 44 | + * @param moduleName String |
| 45 | + */ |
| 46 | + public DbSchemaWhitelistJsonGenerator( |
| 47 | + final @NotNull Project project, |
| 48 | + final @NotNull DbSchemaXmlData dbSchemaXmlData, |
| 49 | + final @NotNull String moduleName |
| 50 | + ) { |
| 51 | + super(project); |
| 52 | + this.project = project; |
| 53 | + jsonElementGenerator = new JsonElementGenerator(project); |
| 54 | + this.dbSchemaXmlData = dbSchemaXmlData; |
| 55 | + this.moduleName = moduleName; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Generate db_schema_whitelist.json file. |
| 60 | + * |
| 61 | + * @param actionName String |
| 62 | + * |
| 63 | + * @return PsiFile |
| 64 | + */ |
| 65 | + @Override |
| 66 | + public PsiFile generate(final String actionName) { |
| 67 | + final FindOrCreateDbSchemaWhitelistJson findOrCreateDbSchemaWhitelistJson = |
| 68 | + new FindOrCreateDbSchemaWhitelistJson(project); |
| 69 | + final JsonFile dbSchemaWhitelist = (JsonFile) |
| 70 | + findOrCreateDbSchemaWhitelistJson.execute(actionName, moduleName); |
| 71 | + |
| 72 | + final JsonElement topNode = dbSchemaWhitelist.getTopLevelValue(); |
| 73 | + |
| 74 | + for (final JsonProperty prop : ((JsonObjectImpl) topNode).getPropertyList()) { |
| 75 | + if (prop.getName().equals(dbSchemaXmlData.getTableName())) { |
| 76 | + return dbSchemaWhitelist; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + WriteCommandAction.runWriteCommandAction(project, () -> { |
| 81 | + final JsonObject tableObject = jsonElementGenerator.createObject(""); |
| 82 | + final JsonObject columnsObject = jsonElementGenerator.createObject(""); |
| 83 | + final JsonObject constraintObject = jsonElementGenerator.createObject(""); |
| 84 | + final JsonObject indexObject = jsonElementGenerator.createObject(""); |
| 85 | + |
| 86 | + for (final Map<String, String> columnData : dbSchemaXmlData.getColumns()) { |
| 87 | + final String columnName = columnData.get(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME); |
| 88 | + final boolean isIdentity = |
| 89 | + Boolean.parseBoolean( |
| 90 | + columnData.get(ModuleDbSchemaXml.XML_ATTR_COLUMN_IDENTITY) |
| 91 | + ); |
| 92 | + |
| 93 | + if (isIdentity) { |
| 94 | + JsonPsiUtil.addProperty( |
| 95 | + constraintObject, |
| 96 | + jsonElementGenerator.createProperty( |
| 97 | + ModuleDbSchemaXml.XML_ATTR_REFERENCE_ID_PK, |
| 98 | + "true" |
| 99 | + ), |
| 100 | + false |
| 101 | + ); |
| 102 | + |
| 103 | + final String indexReferenceId = ModuleDbSchemaXml.generateIndexReferenceId( |
| 104 | + dbSchemaXmlData.getTableName(), |
| 105 | + Arrays.asList(columnName) |
| 106 | + ); |
| 107 | + |
| 108 | + JsonPsiUtil.addProperty( |
| 109 | + indexObject, |
| 110 | + jsonElementGenerator.createProperty( |
| 111 | + indexReferenceId, |
| 112 | + "true" |
| 113 | + ), |
| 114 | + false |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + JsonPsiUtil.addProperty( |
| 119 | + columnsObject, |
| 120 | + jsonElementGenerator.createProperty(columnName, "true"), |
| 121 | + false |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + addChildObjectToObject( |
| 126 | + tableObject, |
| 127 | + ModuleDbSchemaWhitelistJson.COLUMN_OBJECT_NAME, |
| 128 | + columnsObject |
| 129 | + ); |
| 130 | + |
| 131 | + if (!indexObject.getPropertyList().isEmpty()) { |
| 132 | + addChildObjectToObject( |
| 133 | + tableObject, |
| 134 | + ModuleDbSchemaWhitelistJson.INDEX_OBJECT_NAME, |
| 135 | + indexObject |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + if (!constraintObject.getPropertyList().isEmpty()) { |
| 140 | + addChildObjectToObject( |
| 141 | + tableObject, |
| 142 | + ModuleDbSchemaWhitelistJson.CONSTRAINT_OBJECT_NAME, |
| 143 | + constraintObject |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + addChildObjectToObject( |
| 148 | + (JsonObject) topNode, |
| 149 | + dbSchemaXmlData.getTableName(), |
| 150 | + tableObject |
| 151 | + ); |
| 152 | + }); |
| 153 | + |
| 154 | + final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project); |
| 155 | + final Document document = psiDocumentManager.getDocument(dbSchemaWhitelist); |
| 156 | + |
| 157 | + if (document != null) { |
| 158 | + psiDocumentManager.commitDocument(document); |
| 159 | + } |
| 160 | + |
| 161 | + return dbSchemaWhitelist; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Add child object with specified name to the target object. |
| 166 | + * |
| 167 | + * @param target JsonObject |
| 168 | + * @param childName String |
| 169 | + * @param child JsonObject |
| 170 | + */ |
| 171 | + private void addChildObjectToObject( |
| 172 | + final @NotNull JsonObject target, |
| 173 | + final @NotNull String childName, |
| 174 | + final @NotNull JsonObject child |
| 175 | + ) { |
| 176 | + JsonPsiUtil.addProperty( |
| 177 | + target, |
| 178 | + jsonElementGenerator.createProperty( |
| 179 | + childName, |
| 180 | + child.getText() |
| 181 | + ), |
| 182 | + false |
| 183 | + ); |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + @SuppressWarnings("PMD.UncommentedEmptyMethodBody") |
| 188 | + protected void fillAttributes(final Properties attributes) {} |
| 189 | +} |
0 commit comments