77
88import com .google .common .collect .Lists ;
99import com .intellij .openapi .command .WriteCommandAction ;
10- import com .intellij .openapi .project .Project ;
1110import com .intellij .openapi .editor .Document ;
11+ import com .intellij .openapi .project .Project ;
1212import com .intellij .psi .PsiDocumentManager ;
1313import com .intellij .psi .PsiFile ;
14+ import com .intellij .psi .xml .XmlAttribute ;
1415import com .intellij .psi .xml .XmlFile ;
1516import com .intellij .psi .xml .XmlTag ;
17+ import com .intellij .util .indexing .FileBasedIndex ;
1618import com .magento .idea .magento2plugin .actions .generation .data .AclXmlData ;
1719import com .magento .idea .magento2plugin .actions .generation .generator .util .FindOrCreateAclXml ;
1820import com .magento .idea .magento2plugin .magento .files .ModuleAclXml ;
21+ import com .magento .idea .magento2plugin .util .magento .GetAclResourcesTreeUtil ;
1922import java .util .HashMap ;
2023import java .util .LinkedList ;
24+ import java .util .List ;
2125import java .util .Map ;
2226import java .util .Properties ;
23- import java .util .List ;
2427import org .jetbrains .annotations .NotNull ;
2528
2629public class AclXmlGenerator extends FileGenerator {
@@ -43,7 +46,7 @@ public AclXmlGenerator(
4346 final @ NotNull AclXmlData aclXmlData ,
4447 final String moduleName ,
4548 final Project project
46- ) {
49+ ) {
4750 super (project );
4851 this .project = project ;
4952 this .moduleName = moduleName ;
@@ -60,7 +63,9 @@ public PsiFile generate(final String actionName) {
6063 actionName ,
6164 moduleName
6265 );
63- if (aclXml == null ) {
66+ if (aclXml == null
67+ || aclXmlData .getResourceId () == null
68+ || aclXmlData .getResourceTitle () == null ) {
6469 return null ;
6570 }
6671
@@ -80,9 +85,71 @@ public PsiFile generate(final String actionName) {
8085 addSubTagsQueue .add (resourcesTag );
8186 childParentRelationMap .put (resourcesTag , aclTag );
8287 }
83- return commitAclXmlFile (aclXml );
88+
89+ final List <AclXmlData > tree = GetAclResourcesTreeUtil .getInstance ().execute (
90+ project ,
91+ aclXmlData .getParentResourceId ()
92+ );
93+ XmlTag parent = resourcesTag ;
94+ for (final AclXmlData resourceTagData : tree ) {
95+ if (resourceTagData .getParentResourceId () != null ) {
96+ parent = resourcesTag .createChildTag (
97+ ModuleAclXml .XML_TAG_RESOURCE ,
98+ null ,
99+ "" ,
100+ false
101+ );
102+ parent .setAttribute (ModuleAclXml .XML_ATTR_ID , resourceTagData .getResourceId ());
103+ }
104+ parent = createOrGetResourceTag (parent , resourceTagData .getResourceId ());
105+ }
106+ final XmlTag targetTag = parent .createChildTag (
107+ ModuleAclXml .XML_TAG_RESOURCE ,
108+ null ,
109+ null ,
110+ false
111+ );
112+ targetTag .setAttribute (ModuleAclXml .XML_ATTR_ID , aclXmlData .getResourceId ());
113+ targetTag .setAttribute (ModuleAclXml .XML_ATTR_TITLE , aclXmlData .getResourceTitle ());
114+
115+ addSubTagsQueue .add (targetTag );
116+ childParentRelationMap .put (targetTag , parent );
117+ commitAclXmlFile (aclXml );
118+ FileBasedIndex .getInstance ().requestReindex (aclXml .getVirtualFile ());
119+
120+ return aclXml ;
84121 }
85122
123+ /**
124+ * Create new tag or get existing one.
125+ *
126+ * @param parent XmlTag
127+ * @param targetResourceId String
128+ *
129+ * @return XmlTag
130+ */
131+ private XmlTag createOrGetResourceTag (final XmlTag parent , final String targetResourceId ) {
132+ for (final XmlTag tag : parent .getSubTags ()) {
133+ final XmlAttribute idAttribute = tag .getAttribute (ModuleAclXml .XML_ATTR_ID );
134+ if (idAttribute != null && idAttribute .getValue ().equals (targetResourceId )) {
135+ return tag ;
136+ }
137+ }
138+ final XmlTag newTag = parent .createChildTag (ModuleAclXml .XML_TAG_RESOURCE , null , "" , false );
139+ newTag .setAttribute (ModuleAclXml .XML_ATTR_ID , targetResourceId );
140+ addSubTagsQueue .add (newTag );
141+ childParentRelationMap .put (newTag , parent );
142+
143+ return newTag ;
144+ }
145+
146+ /**
147+ * Save XML file.
148+ *
149+ * @param aclXml XmlFile
150+ *
151+ * @return XmlFile
152+ */
86153 private XmlFile commitAclXmlFile (final XmlFile aclXml ) {
87154 WriteCommandAction .runWriteCommandAction (project , () -> {
88155 for (final XmlTag tag : Lists .reverse (addSubTagsQueue )) {
0 commit comments