7
7
8
8
import com .google .common .collect .Lists ;
9
9
import com .intellij .openapi .command .WriteCommandAction ;
10
- import com .intellij .openapi .project .Project ;
11
10
import com .intellij .openapi .editor .Document ;
11
+ import com .intellij .openapi .project .Project ;
12
12
import com .intellij .psi .PsiDocumentManager ;
13
13
import com .intellij .psi .PsiFile ;
14
+ import com .intellij .psi .xml .XmlAttribute ;
14
15
import com .intellij .psi .xml .XmlFile ;
15
16
import com .intellij .psi .xml .XmlTag ;
17
+ import com .intellij .util .indexing .FileBasedIndex ;
16
18
import com .magento .idea .magento2plugin .actions .generation .data .AclXmlData ;
17
19
import com .magento .idea .magento2plugin .actions .generation .generator .util .FindOrCreateAclXml ;
18
20
import com .magento .idea .magento2plugin .magento .files .ModuleAclXml ;
21
+ import com .magento .idea .magento2plugin .util .magento .GetAclResourcesTreeUtil ;
19
22
import java .util .HashMap ;
20
23
import java .util .LinkedList ;
24
+ import java .util .List ;
21
25
import java .util .Map ;
22
26
import java .util .Properties ;
23
- import java .util .List ;
24
27
import org .jetbrains .annotations .NotNull ;
25
28
26
29
public class AclXmlGenerator extends FileGenerator {
@@ -43,7 +46,7 @@ public AclXmlGenerator(
43
46
final @ NotNull AclXmlData aclXmlData ,
44
47
final String moduleName ,
45
48
final Project project
46
- ) {
49
+ ) {
47
50
super (project );
48
51
this .project = project ;
49
52
this .moduleName = moduleName ;
@@ -60,7 +63,9 @@ public PsiFile generate(final String actionName) {
60
63
actionName ,
61
64
moduleName
62
65
);
63
- if (aclXml == null ) {
66
+ if (aclXml == null
67
+ || aclXmlData .getResourceId () == null
68
+ || aclXmlData .getResourceTitle () == null ) {
64
69
return null ;
65
70
}
66
71
@@ -80,9 +85,71 @@ public PsiFile generate(final String actionName) {
80
85
addSubTagsQueue .add (resourcesTag );
81
86
childParentRelationMap .put (resourcesTag , aclTag );
82
87
}
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 ;
84
121
}
85
122
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
+ */
86
153
private XmlFile commitAclXmlFile (final XmlFile aclXml ) {
87
154
WriteCommandAction .runWriteCommandAction (project , () -> {
88
155
for (final XmlTag tag : Lists .reverse (addSubTagsQueue )) {
0 commit comments