2
2
3
3
import java .io .File ;
4
4
import java .io .IOException ;
5
- import java .net .MalformedURLException ;
6
5
import java .nio .file .Files ;
7
6
import java .nio .file .Path ;
8
7
import java .util .Comparator ;
8
+ import java .util .List ;
9
9
import java .util .UUID ;
10
10
11
11
import org .eclipse .core .commands .AbstractHandler ;
27
27
import org .slf4j .LoggerFactory ;
28
28
import org .slf4j .MDC ;
29
29
30
+ import com .devonfw .cobigen .api .TemplateAdapter ;
31
+ import com .devonfw .cobigen .api .constants .ConfigurationConstants ;
32
+ import com .devonfw .cobigen .api .exception .TemplateSelectionForAdaptionException ;
33
+ import com .devonfw .cobigen .api .exception .UpgradeTemplatesNotificationException ;
30
34
import com .devonfw .cobigen .eclipse .common .constants .InfrastructureConstants ;
31
35
import com .devonfw .cobigen .eclipse .common .constants .external .ResourceConstants ;
32
36
import com .devonfw .cobigen .eclipse .common .tools .ExceptionHandler ;
33
- import com .devonfw .cobigen .eclipse .common .tools .PlatformUIUtil ;
34
37
import com .devonfw .cobigen .eclipse .common .tools .ResourcesPluginUtil ;
38
+ import com .devonfw .cobigen .impl .adapter .TemplateAdapterImpl ;
35
39
36
40
/**
37
41
* Handler for the Package-Explorer EventfimportProjectIntoWorkspace
@@ -53,31 +57,23 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
53
57
54
58
MDC .put (InfrastructureConstants .CORRELATION_ID , UUID .randomUUID ().toString ());
55
59
IProject generatorProj = ResourcesPlugin .getWorkspace ().getRoot ().getProject (ResourceConstants .CONFIG_PROJECT_NAME );
56
- IProject generatorProjOfTempltesSets = ResourcesPlugin .getWorkspace ().getRoot ()
57
- .getProject (ResourceConstants .TEMPLATE_SETS_CONFIG_PROJECT_NAME );
58
- Path templateSet = ResourcesPluginUtil .getTemplateSetDirectory ();
59
-
60
- if (generatorProjOfTempltesSets .exists ()) {
61
- // 1. the project is imported so, do not adapt(no need to extract the jar files) For now nothing to do.
62
-
63
- } else if (Files .exists (templateSet )) {
64
60
65
- // 2. downloaded exists? first adapt the jar file (files), then import project to eclipse
66
-
67
- Path downloadedPath = templateSet .resolve (ResourceConstants .TEMPLATE_SETS_DOWNLOADED );
68
- Path adaptedPath = templateSet .resolve (ResourceConstants .TEMPLATE_SETS_ADAPTED );
61
+ Path templateSet = ResourcesPluginUtil .getTemplateSetDirectory ();
62
+ File template = ResourcesPluginUtil .getTemplatesDirectory ();
69
63
70
- // A. adapt the jar files if not already adapted
71
- if (Files .exists (downloadedPath ) && !Files .exists (adaptedPath ))
72
- ResourcesPluginUtil .adaptTemplateSet (templateSet );
64
+ TemplateAdapter templateAdapter ;
73
65
74
- // B. Import the project
75
- importProjectIntoWorkspace (ResourceConstants .TEMPLATE_SETS_CONFIG_PROJECT_NAME , templateSet );
76
- /*
77
- * 3. TODO downloaded does not exists? update command must be executed. then go to 2. // (step 3 can be ignored
78
- * for now until the new template-sets are deployed online.)
79
- */
66
+ if (Files .exists (templateSet )) {
67
+ templateAdapter = new TemplateAdapterImpl (templateSet );
68
+ } else if (template .exists ()) {
69
+ templateAdapter = new TemplateAdapterImpl (template .toPath ());
80
70
} else {
71
+ LOG .debug ("No configuration project was found" );
72
+ return null ;
73
+ }
74
+ try {
75
+ templateAdapter .adaptTemplates ();
76
+
81
77
if (generatorProj .exists ()) {
82
78
MessageDialog dialog = new MessageDialog (Display .getDefault ().getActiveShell (), "Info!" , null ,
83
79
"CobiGen_Templates folder is already imported, click on Update templates button to update with latest. " ,
@@ -94,40 +90,48 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
94
90
95
91
if (result == 0 ) {
96
92
try {
97
- String fileName = ResourcesPluginUtil .getJarPath (true );
98
- if (fileName .equals ("" )) {
99
- result = createUpdateTemplatesDialog ();
100
- if (result == 1 ) {
101
- MessageDialog .openWarning (Display .getDefault ().getActiveShell (), "Warning" ,
102
- "Templates have not been found, please download them!" );
103
- throw new NullPointerException ("Templates have not been found!" );
104
- } else {
105
- fileName = ResourcesPluginUtil .downloadJar (true );
106
- }
107
-
108
- }
109
- ResourcesPluginUtil .processJar (fileName );
110
-
111
93
importProjectIntoWorkspace (ResourceConstants .CONFIG_PROJECT_NAME ,
112
94
ResourcesPluginUtil .getTemplatesDirectory ().toPath ());
113
95
dialog = new MessageDialog (Display .getDefault ().getActiveShell (), "Information" , null ,
114
96
"CobiGen_Templates folder is imported sucessfully" , MessageDialog .INFORMATION , new String [] { "Ok" },
115
97
1 );
116
98
dialog .setBlockOnOpen (true );
117
99
dialog .open ();
118
- } catch (MalformedURLException e ) {
119
- LOG .error ("An exception with download url of maven central" , e );
120
- PlatformUIUtil .openErrorDialog ("An exception with download url of maven central" , e );
121
- } catch (IOException e ) {
122
- LOG .error ("An exception occurred while writing Jar files to .metadata folder" , e );
123
- PlatformUIUtil .openErrorDialog ("An exception occurred while writing Jar files to .metadata folder" , e );
124
100
} catch (Throwable e ) {
125
101
ExceptionHandler .handle (e , HandlerUtil .getActiveShell (event ));
126
102
}
127
103
}
128
104
MDC .remove (InfrastructureConstants .CORRELATION_ID );
129
105
}
106
+
107
+ } catch (TemplateSelectionForAdaptionException e ) {
108
+ List <Path > templateJars = e .getTemplateSets ();
109
+ if (!templateJars .isEmpty ()) {
110
+
111
+ try {
112
+ // Adapt process
113
+ templateAdapter .adaptTemplateSets (templateJars , false );
114
+
115
+ // B. Import into Eclipse
116
+ importProjectIntoWorkspace (ResourceConstants .TEMPLATE_SETS_CONFIG_PROJECT_NAME , templateSet );
117
+ MessageDialog dialog = new MessageDialog (Display .getDefault ().getActiveShell (), "Information" , null ,
118
+ "CobiGen_Templates folder is imported sucessfully" , MessageDialog .INFORMATION , new String [] { "Ok" }, 1 );
119
+ dialog .setBlockOnOpen (true );
120
+ dialog .open ();
121
+
122
+ } catch (IOException e1 ) {
123
+ LOG .debug ("Adapt was not possible" , e1 );
124
+ }
125
+
126
+ } else {
127
+ LOG .info ("No template set jars found to extract." );
128
+ }
129
+ } catch (IOException e ) {
130
+ LOG .debug ("Error occur" , e );
131
+ } catch (UpgradeTemplatesNotificationException e ) {
132
+ LOG .debug ("Upgrade Process" , e );
130
133
}
134
+
131
135
return null ;
132
136
133
137
}
@@ -150,7 +154,7 @@ private void importProjectIntoWorkspace(String projectName, Path projectPath) {
150
154
if (projectName == ResourceConstants .TEMPLATE_SETS_CONFIG_PROJECT_NAME ) {
151
155
description .setLocationURI (projectPath .toUri ());
152
156
} else {
153
- description .setLocation ( new org . eclipse . core . runtime . Path ( this . ws . toPortableString () + "/" + projectName ));
157
+ description .setLocationURI ( projectPath . resolve ( ConfigurationConstants . COBIGEN_TEMPLATES ). toUri ( ));
154
158
}
155
159
project .create (description , null );
156
160
0 commit comments