2
2
* Copyright © Magento, Inc. All rights reserved.
3
3
* See COPYING.txt for license details.
4
4
*/
5
+
5
6
package com .magento .idea .magento2plugin .actions .generation .generator ;
6
7
7
8
import com .intellij .openapi .command .WriteCommandAction ;
17
18
import com .intellij .psi .xml .XmlTag ;
18
19
import com .magento .idea .magento2plugin .actions .generation .data .CrontabXmlData ;
19
20
import com .magento .idea .magento2plugin .actions .generation .generator .util .FindOrCreateCrontabXml ;
20
- import com .magento .idea .magento2plugin .actions .generation .generator .util .GetCodeTemplate ;
21
+ import com .magento .idea .magento2plugin .actions .generation .generator .util .GetCodeTemplateUtil ;
21
22
import com .magento .idea .magento2plugin .actions .generation .generator .util .XmlFilePositionUtil ;
22
23
import com .magento .idea .magento2plugin .magento .files .CrontabXmlTemplate ;
23
24
import com .magento .idea .magento2plugin .util .xml .XmlPsiTreeUtil ;
24
- import org .jetbrains .annotations .NotNull ;
25
25
import java .io .IOException ;
26
26
import java .util .Collection ;
27
27
import java .util .Properties ;
28
+ import org .jetbrains .annotations .NotNull ;
28
29
29
30
public class CrontabXmlGenerator extends FileGenerator {
30
- private Project project ;
31
- private CrontabXmlData crontabXmlData ;
31
+ private final Project project ;
32
+ private final CrontabXmlData crontabXmlData ;
32
33
private boolean isCronGroupDeclared ;
33
34
34
- private GetCodeTemplate getCodeTemplate ;
35
- private CodeStyleManager codeStyleManager ;
36
- private FindOrCreateCrontabXml findOrCreateCrontabXml ;
37
- private XmlFilePositionUtil positionUtil ;
38
- private PsiDocumentManager psiDocumentManager ;
35
+ private final GetCodeTemplateUtil getCodeTemplateUtil ;
36
+ private final CodeStyleManager codeStyleManager ;
37
+ private final FindOrCreateCrontabXml findOrCreateCrontabXml ;
38
+ private final XmlFilePositionUtil positionUtil ;
39
+ private final PsiDocumentManager psiDocumentManager ;
39
40
40
- public CrontabXmlGenerator (Project project , @ NotNull CrontabXmlData crontabXmlData ) {
41
+ /**
42
+ * Generator for crontab.xml.
43
+ *
44
+ * @param project Project
45
+ * @param crontabXmlData CrontabXmlData
46
+ */
47
+ public CrontabXmlGenerator (
48
+ final Project project ,
49
+ final @ NotNull CrontabXmlData crontabXmlData
50
+ ) {
41
51
super (project );
42
52
43
53
this .project = project ;
44
54
this .crontabXmlData = crontabXmlData ;
45
55
46
56
this .findOrCreateCrontabXml = new FindOrCreateCrontabXml (project );
47
- this .getCodeTemplate = GetCodeTemplate . getInstance (project );
57
+ this .getCodeTemplateUtil = new GetCodeTemplateUtil (project );
48
58
this .psiDocumentManager = PsiDocumentManager .getInstance (project );
49
59
this .codeStyleManager = CodeStyleManager .getInstance (project );
50
60
this .positionUtil = XmlFilePositionUtil .getInstance ();
51
61
}
52
62
53
63
/**
54
- * Register newly created cronjob in the crontab.xml. If there is not crontab.xml, it will create one.
64
+ * Register newly created cronjob in the crontab.xml.
65
+ * If there is not crontab.xml, it will create one.
55
66
*
56
- * @param actionName
67
+ * @param actionName String
57
68
*
58
69
* @return PsiFile
59
70
*/
60
- public PsiFile generate (String actionName ) {
61
- String moduleName = this .crontabXmlData .getModuleName ();
71
+ @ Override
72
+ public PsiFile generate (final String actionName ) {
73
+ final String moduleName = this .crontabXmlData .getModuleName ();
62
74
63
- XmlFile crontabXmlFile = (XmlFile ) this .findOrCreateCrontabXml .execute (
64
- actionName ,
65
- moduleName
75
+ final XmlFile crontabXmlFile = (XmlFile ) this .findOrCreateCrontabXml .execute (
76
+ actionName ,
77
+ moduleName
66
78
);
67
79
68
- String cronjobGroup = this .crontabXmlData .getCronGroup ();
69
- String cronjobName = this .crontabXmlData .getCronjobName ();
70
- XmlTag cronGroupTag = this .getCronGroupTag (crontabXmlFile , cronjobGroup );
80
+ final String cronjobGroup = this .crontabXmlData .getCronGroup ();
81
+ final String cronjobName = this .crontabXmlData .getCronjobName ();
82
+ final XmlTag cronGroupTag = this .getCronGroupTag (crontabXmlFile , cronjobGroup );
71
83
72
84
this .isCronGroupDeclared = false ;
73
85
boolean isCronjobDeclared = false ;
@@ -78,46 +90,49 @@ public PsiFile generate(String actionName) {
78
90
}
79
91
80
92
if (isCronjobDeclared ) {
81
- throw new RuntimeException (cronjobName +" cronjob is already declared in the " + moduleName + " module" );
93
+ throw new RuntimeException (cronjobName //NOPMD
94
+ + " cronjob is already declared in the " + moduleName + " module" );
82
95
}
83
96
84
97
WriteCommandAction .runWriteCommandAction (project , () -> {
85
- StringBuffer textBuf = new StringBuffer ();
98
+ final StringBuffer textBuf = new StringBuffer ();
86
99
87
100
try {
88
- String cronjobRegistrationTemplate = this .getCodeTemplate .execute (
89
- CrontabXmlTemplate .TEMPLATE_CRONJOB_REGISTRATION ,
90
- getAttributes ()
101
+ final String cronjobRegistrationTemplate = this .getCodeTemplateUtil .execute (
102
+ CrontabXmlTemplate .TEMPLATE_CRONJOB_REGISTRATION ,
103
+ getAttributes ()
91
104
);
92
105
93
106
textBuf .append (cronjobRegistrationTemplate );
94
107
} catch (IOException e ) {
95
- e .printStackTrace ();
108
+ e .printStackTrace ();//NOPMD
96
109
return ;
97
110
}
98
111
99
- int insertPos = this .isCronGroupDeclared
112
+ final int insertPos = this .isCronGroupDeclared
100
113
? this .positionUtil .getEndPositionOfTag (cronGroupTag )
101
114
: this .positionUtil .getRootInsertPosition (crontabXmlFile );
102
115
103
116
if (textBuf .length () > 0 && insertPos >= 0 ) {
104
- Document document = this .psiDocumentManager .getDocument (crontabXmlFile );
117
+ final Document document = this .psiDocumentManager .getDocument (crontabXmlFile );
105
118
106
119
if (document == null ) {
107
120
// practically this should not be possible as we tell to edit XML file
108
- throw new RuntimeException (
109
- crontabXmlFile .getVirtualFile ().getPath () + " file is binary or has no document associations"
121
+ throw new RuntimeException (//NOPMD
122
+ crontabXmlFile .getVirtualFile ().getPath ()
123
+ + " file is binary or has no document associations"
110
124
);
111
125
}
112
126
113
127
if (!document .isWritable ()) {
114
- throw new RuntimeException (
115
- crontabXmlFile .getVirtualFile ().getPath () + " file is not writable. Please check file permission"
128
+ throw new RuntimeException (//NOPMD
129
+ crontabXmlFile .getVirtualFile ().getPath ()
130
+ + " file is not writable. Please check file permission"
116
131
);
117
132
}
118
133
119
134
document .insertString (insertPos , textBuf );
120
- int endPos = insertPos + textBuf .length () + 1 ;
135
+ final int endPos = insertPos + textBuf .length () + 1 ;
121
136
122
137
this .codeStyleManager .reformatText (crontabXmlFile , insertPos , endPos );
123
138
this .psiDocumentManager .commitDocument (document );
@@ -127,12 +142,11 @@ public PsiFile generate(String actionName) {
127
142
return crontabXmlFile ;
128
143
}
129
144
130
- protected void fillAttributes (Properties attributes ) {
131
- String cronjobName = this .crontabXmlData .getCronjobName ();
132
- String cronjobGroup = this .crontabXmlData .getCronGroup ();
133
- String cronjobInstance = this .crontabXmlData .getCronjobInstance ();
134
- String cronjobSchedule = this .crontabXmlData .getCronjobSchedule ();
135
- String cronjobScheduleConfigPath = this .crontabXmlData .getCronjobScheduleConfigPath ();
145
+ @ Override
146
+ protected void fillAttributes (final Properties attributes ) {
147
+ final String cronjobGroup = this .crontabXmlData .getCronGroup ();
148
+ final String cronjobSchedule = this .crontabXmlData .getCronjobSchedule ();
149
+ final String cronjobScheduleConfigPath = this .crontabXmlData .getCronjobScheduleConfigPath ();
136
150
137
151
if (!this .isCronGroupDeclared ) {
138
152
attributes .setProperty ("CRON_GROUP" , cronjobGroup );
@@ -146,36 +160,43 @@ protected void fillAttributes(Properties attributes) {
146
160
attributes .setProperty ("CRONJOB_SCHEDULE_CONFIG_PATH" , cronjobScheduleConfigPath );
147
161
}
148
162
163
+ final String cronjobName = this .crontabXmlData .getCronjobName ();
149
164
attributes .setProperty ("CRONJOB_NAME" , cronjobName );
165
+
166
+ final String cronjobInstance = this .crontabXmlData .getCronjobInstance ();
150
167
attributes .setProperty ("CRONJOB_INSTANCE" , cronjobInstance );
151
168
}
152
169
153
170
/**
154
- * Check whenever cronjob with cronjobName is declared under cronGroupTag
171
+ * Check whenever cronjob with cronjobName is declared under cronGroupTag.
155
172
*
156
- * @param cronGroupTag
157
- * @param cronjobName
173
+ * @param cronGroupTag XmlTag
174
+ * @param cronjobName String
158
175
*
159
176
* @return boolean
160
177
*/
161
- private boolean isCronjobDeclared (XmlTag cronGroupTag , String cronjobName ) {
162
- XmlTag [] cronjobTags = PsiTreeUtil .getChildrenOfType (cronGroupTag , XmlTag .class );
178
+ private boolean isCronjobDeclared (final XmlTag cronGroupTag , final String cronjobName ) {
179
+ final XmlTag [] cronjobTags = PsiTreeUtil .getChildrenOfType (cronGroupTag , XmlTag .class );
163
180
164
181
if (cronjobTags == null ) {
165
182
return false ;
166
183
}
167
184
168
- for (XmlTag cronjobTag : cronjobTags ) {
185
+ for (final XmlTag cronjobTag : cronjobTags ) {
169
186
if (!cronjobTag .getName ().equals (CrontabXmlTemplate .CRON_JOB_TAG )) {
170
187
continue ;
171
188
}
172
189
173
- XmlAttribute [] cronjobAttributes = PsiTreeUtil .getChildrenOfType (cronjobTag , XmlAttribute .class );
190
+ final XmlAttribute [] cronjobAttributes = PsiTreeUtil .getChildrenOfType (
191
+ cronjobTag ,
192
+ XmlAttribute .class
193
+ );
174
194
175
195
// todo: handle null pointer
176
196
177
- for (XmlAttribute cronjobAttribute : cronjobAttributes ) {
178
- if (!cronjobAttribute .getName ().equals (CrontabXmlTemplate .CRON_JOB_NAME_ATTRIBUTE )) {
197
+ for (final XmlAttribute cronjobAttribute : cronjobAttributes ) {
198
+ if (!cronjobAttribute .getName ()
199
+ .equals (CrontabXmlTemplate .CRON_JOB_NAME_ATTRIBUTE )) {
179
200
continue ;
180
201
}
181
202
@@ -189,26 +210,27 @@ private boolean isCronjobDeclared(XmlTag cronGroupTag, String cronjobName) {
189
210
}
190
211
191
212
/**
192
- * Retrieve cronGroup tag with cronjobGroup name if it registered in crontabXmlFile
213
+ * Retrieve cronGroup tag with cronjobGroup name if it registered in crontabXmlFile.
193
214
*
194
- * @param crontabXmlFile
195
- * @param cronjobGroup
215
+ * @param crontabXmlFile XmlFile
216
+ * @param cronjobGroup String
196
217
*
197
218
* @return XmlTag
198
219
*/
199
- private XmlTag getCronGroupTag (XmlFile crontabXmlFile , String cronjobGroup ) {
200
- Collection <XmlAttributeValue > cronGroupIdAttributes = XmlPsiTreeUtil .findAttributeValueElements (
201
- crontabXmlFile ,
202
- CrontabXmlTemplate .CRON_GROUP_TAG ,
203
- CrontabXmlTemplate .CRON_GROUP_NAME_ATTRIBUTE
220
+ private XmlTag getCronGroupTag (final XmlFile crontabXmlFile , final String cronjobGroup ) {
221
+ final Collection <XmlAttributeValue > cronGroupIdAttributes
222
+ = XmlPsiTreeUtil .findAttributeValueElements (
223
+ crontabXmlFile ,
224
+ CrontabXmlTemplate .CRON_GROUP_TAG ,
225
+ CrontabXmlTemplate .CRON_GROUP_NAME_ATTRIBUTE
204
226
);
205
227
206
- for (XmlAttributeValue cronGroupIdAttribute : cronGroupIdAttributes ) {
228
+ for (final XmlAttributeValue cronGroupIdAttribute : cronGroupIdAttributes ) {
207
229
if (!cronGroupIdAttribute .getValue ().equals (cronjobGroup )) {
208
230
continue ;
209
231
}
210
232
211
- return PsiTreeUtil .getParentOfType (cronGroupIdAttribute , XmlTag .class );
233
+ return PsiTreeUtil .getParentOfType (cronGroupIdAttribute , XmlTag .class );//NOPMD
212
234
}
213
235
214
236
return null ;
0 commit comments