22 * Copyright © Magento, Inc. All rights reserved.
33 * See COPYING.txt for license details.
44 */
5+
56package com .magento .idea .magento2plugin .actions .generation .generator ;
67
78import com .intellij .openapi .command .WriteCommandAction ;
1718import com .intellij .psi .xml .XmlTag ;
1819import com .magento .idea .magento2plugin .actions .generation .data .CrontabXmlData ;
1920import 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 ;
2122import com .magento .idea .magento2plugin .actions .generation .generator .util .XmlFilePositionUtil ;
2223import com .magento .idea .magento2plugin .magento .files .CrontabXmlTemplate ;
2324import com .magento .idea .magento2plugin .util .xml .XmlPsiTreeUtil ;
24- import org .jetbrains .annotations .NotNull ;
2525import java .io .IOException ;
2626import java .util .Collection ;
2727import java .util .Properties ;
28+ import org .jetbrains .annotations .NotNull ;
2829
2930public class CrontabXmlGenerator extends FileGenerator {
30- private Project project ;
31- private CrontabXmlData crontabXmlData ;
31+ private final Project project ;
32+ private final CrontabXmlData crontabXmlData ;
3233 private boolean isCronGroupDeclared ;
3334
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 ;
3940
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+ ) {
4151 super (project );
4252
4353 this .project = project ;
4454 this .crontabXmlData = crontabXmlData ;
4555
4656 this .findOrCreateCrontabXml = new FindOrCreateCrontabXml (project );
47- this .getCodeTemplate = GetCodeTemplate . getInstance (project );
57+ this .getCodeTemplateUtil = new GetCodeTemplateUtil (project );
4858 this .psiDocumentManager = PsiDocumentManager .getInstance (project );
4959 this .codeStyleManager = CodeStyleManager .getInstance (project );
5060 this .positionUtil = XmlFilePositionUtil .getInstance ();
5161 }
5262
5363 /**
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.
5566 *
56- * @param actionName
67+ * @param actionName String
5768 *
5869 * @return PsiFile
5970 */
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 ();
6274
63- XmlFile crontabXmlFile = (XmlFile ) this .findOrCreateCrontabXml .execute (
64- actionName ,
65- moduleName
75+ final XmlFile crontabXmlFile = (XmlFile ) this .findOrCreateCrontabXml .execute (
76+ actionName ,
77+ moduleName
6678 );
6779
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 );
7183
7284 this .isCronGroupDeclared = false ;
7385 boolean isCronjobDeclared = false ;
@@ -78,46 +90,49 @@ public PsiFile generate(String actionName) {
7890 }
7991
8092 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" );
8295 }
8396
8497 WriteCommandAction .runWriteCommandAction (project , () -> {
85- StringBuffer textBuf = new StringBuffer ();
98+ final StringBuffer textBuf = new StringBuffer ();
8699
87100 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 ()
91104 );
92105
93106 textBuf .append (cronjobRegistrationTemplate );
94107 } catch (IOException e ) {
95- e .printStackTrace ();
108+ e .printStackTrace ();//NOPMD
96109 return ;
97110 }
98111
99- int insertPos = this .isCronGroupDeclared
112+ final int insertPos = this .isCronGroupDeclared
100113 ? this .positionUtil .getEndPositionOfTag (cronGroupTag )
101114 : this .positionUtil .getRootInsertPosition (crontabXmlFile );
102115
103116 if (textBuf .length () > 0 && insertPos >= 0 ) {
104- Document document = this .psiDocumentManager .getDocument (crontabXmlFile );
117+ final Document document = this .psiDocumentManager .getDocument (crontabXmlFile );
105118
106119 if (document == null ) {
107120 // 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"
110124 );
111125 }
112126
113127 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"
116131 );
117132 }
118133
119134 document .insertString (insertPos , textBuf );
120- int endPos = insertPos + textBuf .length () + 1 ;
135+ final int endPos = insertPos + textBuf .length () + 1 ;
121136
122137 this .codeStyleManager .reformatText (crontabXmlFile , insertPos , endPos );
123138 this .psiDocumentManager .commitDocument (document );
@@ -127,12 +142,11 @@ public PsiFile generate(String actionName) {
127142 return crontabXmlFile ;
128143 }
129144
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 ();
136150
137151 if (!this .isCronGroupDeclared ) {
138152 attributes .setProperty ("CRON_GROUP" , cronjobGroup );
@@ -146,36 +160,43 @@ protected void fillAttributes(Properties attributes) {
146160 attributes .setProperty ("CRONJOB_SCHEDULE_CONFIG_PATH" , cronjobScheduleConfigPath );
147161 }
148162
163+ final String cronjobName = this .crontabXmlData .getCronjobName ();
149164 attributes .setProperty ("CRONJOB_NAME" , cronjobName );
165+
166+ final String cronjobInstance = this .crontabXmlData .getCronjobInstance ();
150167 attributes .setProperty ("CRONJOB_INSTANCE" , cronjobInstance );
151168 }
152169
153170 /**
154- * Check whenever cronjob with cronjobName is declared under cronGroupTag
171+ * Check whenever cronjob with cronjobName is declared under cronGroupTag.
155172 *
156- * @param cronGroupTag
157- * @param cronjobName
173+ * @param cronGroupTag XmlTag
174+ * @param cronjobName String
158175 *
159176 * @return boolean
160177 */
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 );
163180
164181 if (cronjobTags == null ) {
165182 return false ;
166183 }
167184
168- for (XmlTag cronjobTag : cronjobTags ) {
185+ for (final XmlTag cronjobTag : cronjobTags ) {
169186 if (!cronjobTag .getName ().equals (CrontabXmlTemplate .CRON_JOB_TAG )) {
170187 continue ;
171188 }
172189
173- XmlAttribute [] cronjobAttributes = PsiTreeUtil .getChildrenOfType (cronjobTag , XmlAttribute .class );
190+ final XmlAttribute [] cronjobAttributes = PsiTreeUtil .getChildrenOfType (
191+ cronjobTag ,
192+ XmlAttribute .class
193+ );
174194
175195 // todo: handle null pointer
176196
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 )) {
179200 continue ;
180201 }
181202
@@ -189,26 +210,27 @@ private boolean isCronjobDeclared(XmlTag cronGroupTag, String cronjobName) {
189210 }
190211
191212 /**
192- * Retrieve cronGroup tag with cronjobGroup name if it registered in crontabXmlFile
213+ * Retrieve cronGroup tag with cronjobGroup name if it registered in crontabXmlFile.
193214 *
194- * @param crontabXmlFile
195- * @param cronjobGroup
215+ * @param crontabXmlFile XmlFile
216+ * @param cronjobGroup String
196217 *
197218 * @return XmlTag
198219 */
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
204226 );
205227
206- for (XmlAttributeValue cronGroupIdAttribute : cronGroupIdAttributes ) {
228+ for (final XmlAttributeValue cronGroupIdAttribute : cronGroupIdAttributes ) {
207229 if (!cronGroupIdAttribute .getValue ().equals (cronjobGroup )) {
208230 continue ;
209231 }
210232
211- return PsiTreeUtil .getParentOfType (cronGroupIdAttribute , XmlTag .class );
233+ return PsiTreeUtil .getParentOfType (cronGroupIdAttribute , XmlTag .class );//NOPMD
212234 }
213235
214236 return null ;
0 commit comments