22
33import com .intellij .openapi .ui .Messages ;
44import com .squareup .javapoet .*;
5+ import com .techconative .actions .DozerTOMapperStructPlugin ;
56import com .techconative .actions .utilities .Utilities ;
67import org .mapstruct .Mapper ;
78import org .mapstruct .Mapping ;
1415import org .xml .sax .SAXException ;
1516
1617import javax .lang .model .element .Modifier ;
18+ import javax .swing .text .BadLocationException ;
1719import javax .xml .parsers .DocumentBuilder ;
1820import javax .xml .parsers .DocumentBuilderFactory ;
1921import javax .xml .parsers .ParserConfigurationException ;
2022import java .io .IOException ;
2123import java .io .StringReader ;
24+ import java .nio .file .FileSystems ;
2225import java .nio .file .Paths ;
2326import java .util .*;
27+ import java .util .concurrent .atomic .AtomicBoolean ;
2428import java .util .stream .IntStream ;
2529
2630public class GenerateMappings {
2731
2832 static private TypeSpec .Builder person ;
2933 static private boolean alreadyExecuted = false ;
3034 private static Document finalDocument ;
35+ private static int length ;
3136
32- public static String generateMappings ( String selectedText , String path , boolean generate ,
33- String className , String mapperName ) throws IOException {
37+
38+ public static Integer check ( String selectedText ) {
3439 DocumentBuilderFactory dbFactory = DocumentBuilderFactory .newInstance ();
35- Map <String , String > map = new HashMap <>();
3640 DocumentBuilder dBuilder = null ;
41+ length = 0 ;
3742 try {
3843 dBuilder = dbFactory .newDocumentBuilder ();
3944 } catch (ParserConfigurationException ex ) {
4045 Messages .showMessageDialog (String .valueOf (ex ), "ERROR" , Messages .getErrorIcon ());
41- return null ;
46+ return 0 ;
4247 }
4348 finalDocument = null ;
4449 try {
4550 finalDocument = dBuilder .parse (new InputSource (new StringReader (selectedText )));
4651 } catch (SAXException | IOException | NullPointerException ex ) {
4752 Messages .showMessageDialog (String .valueOf (ex ), "ERROR" , Messages .getErrorIcon ());
48- return null ;
53+ return 0 ;
4954 }
5055 finalDocument .getDocumentElement ().normalize ();
56+ return finalDocument .getElementsByTagName ("mapping" ).getLength ();
57+ }
5158
52- int length = finalDocument .getElementsByTagName ("mapping" ).getLength ();
59+ public static boolean CheckXml (String selectedText ) {
60+ length = check (selectedText );
61+ if (length == 1 && finalDocument .getElementsByTagName ("mappings" ).getLength () == 0 ) {
62+ alreadyExecuted = true ;
63+ return true ;
64+ } else {
65+ alreadyExecuted = false ;
66+ return false ;
67+ }
68+ }
69+
70+ public static String generateMappings (String selectedText , String path , boolean generate ,
71+ String className , String mapperName ) throws IOException , BadLocationException {
72+ if (length == 0 ) {
73+ return null ;
74+ }
75+ Map <String , String > map = new HashMap <>();
76+ person = null ;
77+ AtomicBoolean partialMapping = new AtomicBoolean (false );
5378
5479 if (length != finalDocument .getElementsByTagName ("class-a" ).getLength () &&
5580 length != finalDocument .getElementsByTagName ("class-b" ).getLength ()) {
5681 Messages .showMessageDialog ("Wrong xml structure" , "ERROR" , Messages .getErrorIcon ());
5782 return null ;
5883 }
5984
60-
6185 IntStream .range (0 , length ).forEachOrdered (x -> {
6286 map .clear ();
6387 NodeList nodeList = finalDocument .getElementsByTagName ("mapping" ).item (x ).getChildNodes ();
@@ -99,13 +123,26 @@ public static String generateMappings(String selectedText, String path, boolean
99123 if (finalDocument .getElementsByTagName ("mapping" ).item (x ).getAttributes ().getNamedItem ("map-id" ) != null ) {
100124 String mapId = finalDocument .getElementsByTagName ("mapping" ).item (x ).getAttributes ()
101125 .getNamedItem ("map-id" ).getTextContent ();
102- person .addAnnotation (AnnotationSpec .builder (Named .class )
103- .addMember ("value" , "$S" , Utilities .apply (mapId )).build ());
126+ map .put ("methodMapId" , mapId );
127+ }
128+ if (finalDocument .getElementsByTagName ("mappings" ).getLength () == 0 && length >= 1 && !generate ) {
129+ try {
130+ DozerTOMapperStructPlugin .getJTextPlane (generateMethod (map , annotationSpecList , true )
131+ .replaceAll ("@org.mapstruct." , "@" ));
132+ partialMapping .set (true );
133+ } catch (BadLocationException e ) {
134+ throw new RuntimeException (e );
135+ }
136+ } else {
137+ generateMethod (map , annotationSpecList , false );
104138 }
105- generateMethod (map , annotationSpecList );
106139 });
107-
108- return generateJavaClass (path , generate );
140+ if (partialMapping .get ()) {
141+ alreadyExecuted = false ;
142+ return null ;
143+ } else {
144+ return generateJavaClass (path , generate );
145+ }
109146 }
110147
111148
@@ -119,7 +156,7 @@ private static String getClassName(String value) {
119156 return strings [strings .length - 1 ];
120157 }
121158
122- static void generateMethod (Map <String , String > map , List <AnnotationSpec > annotationSpecList ) {
159+ static String generateMethod (Map <String , String > map , List <AnnotationSpec > annotationSpecList , boolean partialMapping ) {
123160
124161 ClassName classTypeB = ClassName .get (map .get ("packageB" ), map .get ("ClassBName" ));
125162 ClassName classTypeA = ClassName .get (map .get ("packageA" ), map .get ("ClassAName" ));
@@ -128,16 +165,24 @@ static void generateMethod(Map<String, String> map, List<AnnotationSpec> annotat
128165 .methodBuilder ("to" + map .get ("ClassBName" ))
129166 .addParameter (classTypeA , Utilities .getObjectNameForClassName (map .get ("ClassAName" )))
130167 .returns (classTypeB ).addModifiers (Modifier .PUBLIC , Modifier .ABSTRACT );
131- AnnotationSpec .Builder anno = AnnotationSpec .builder (Mappings .class );
132- IntStream .range (0 , annotationSpecList .size ()).forEachOrdered (x ->
133- anno .addMember ("value" , "$L" , annotationSpecList .get (x ))
134- );
135- method .addAnnotation (anno .build ());
168+ if (!annotationSpecList .isEmpty () && annotationSpecList != null ) {
169+ AnnotationSpec .Builder anno = AnnotationSpec .builder (Mappings .class );
170+ IntStream .range (0 , annotationSpecList .size ()).forEachOrdered (x ->
171+ anno .addMember ("value" , "$L" , annotationSpecList .get (x ))
172+ );
173+ method .addAnnotation (anno .build ());
174+ }
136175 if (map .containsKey ("methodMapId" )) {
137176 method .addAnnotation (AnnotationSpec .builder (Named .class )
138177 .addMember ("value" , "$S" , Utilities .findAndApply (map .get ("methodMapId" ))).build ());
139178 }
140- person .addMethod (method .build ());
179+ if (partialMapping ) {
180+ return method .build ().toString ().replaceAll (map .get ("packageB" ) + "." , "" )
181+ .replaceAll (map .get ("packageA" ) + "." , "" );
182+ } else {
183+ person .addMethod (method .build ());
184+ return null ;
185+ }
141186 }
142187
143188 static private void buildJavaClass (String path , Map <String , String > map , String className , String mapperName ) {
@@ -177,10 +222,12 @@ static private String generateJavaClass(String path, boolean generate) throws IO
177222 return code ;
178223 }
179224
180- static private String [] getPath (String path ) {
181- String str = path .replace (path .charAt (2 ), '.' );
182- String [] strings = str .split ("src.main.java." );
183- strings [0 ] = str .replaceAll ("." + strings [1 ], "" ).trim ().replace ('.' , path .charAt (2 ));
225+ private static String [] getPath (String path ) {
226+ String fileSeparator = FileSystems .getDefault ().getSeparator ();
227+ String str = path .replace (fileSeparator , "." );
228+ String [] strings = new String [2 ];
229+ strings [1 ] = str .split (".src.main.java." )[1 ];
230+ strings [0 ] = path .replace (strings [1 ].replace ("." , fileSeparator ), "" );
184231 return strings ;
185232 }
186233
0 commit comments