11package com .aliyun .dataworks .migrationx .domain .dataworks .service .converter ;
22
33import java .nio .file .Paths ;
4+ import java .util .List ;
45import java .util .Objects ;
56import java .util .Optional ;
67import java .util .stream .Collectors ;
1011import com .aliyun .dataworks .common .spec .domain .SpecRefEntity ;
1112import com .aliyun .dataworks .common .spec .domain .Specification ;
1213import com .aliyun .dataworks .common .spec .domain .dw .nodemodel .DataWorksNodeAdapter ;
14+ import com .aliyun .dataworks .common .spec .domain .dw .nodemodel .DataWorksNodeAdapter .Context ;
1315import com .aliyun .dataworks .common .spec .domain .dw .nodemodel .DwNodeDependentTypeInfo ;
1416import com .aliyun .dataworks .common .spec .domain .dw .nodemodel .OutputContext ;
1517import com .aliyun .dataworks .common .spec .domain .dw .types .CodeProgramType ;
4547import lombok .extern .slf4j .Slf4j ;
4648import org .apache .commons .collections4 .CollectionUtils ;
4749import org .apache .commons .collections4 .ListUtils ;
50+ import org .apache .commons .collections4 .MapUtils ;
4851import org .apache .commons .lang3 .StringUtils ;
4952
5053/**
@@ -99,6 +102,46 @@ public static FileDetail functionSpecToFileDetail(Specification<DataWorksWorkflo
99102 return functionSpecToFileDetail (spec , null );
100103 }
101104
105+ public static FileDetail componentSpecToFileDetail (Specification <DataWorksWorkflowSpec > spec ) {
106+ return componentSpecToFileDetail (spec , null );
107+ }
108+
109+ private static FileDetail componentSpecToFileDetail (Specification <DataWorksWorkflowSpec > spec , String resourceId ) {
110+ FileDetail fileDetail = new FileDetail ();
111+ File file = componentSpecToFile (spec , resourceId );
112+ if (file == null ) {
113+ log .error ("get file from function spec is null" );
114+ return null ;
115+ }
116+
117+ fileDetail .setFile (file );
118+ fileDetail .setNodeCfg (initFileNodeCfgByFile (file ));
119+ return fileDetail ;
120+ }
121+
122+ private static File componentSpecToFile (Specification <DataWorksWorkflowSpec > spec , String functionId ) {
123+ DataWorksWorkflowSpec dataWorksWorkflowSpec = spec .getSpec ();
124+ if (spec .getSpec () == null ) {
125+ log .warn ("dataworks component spec is null" );
126+ return null ;
127+ }
128+
129+ return ListUtils .emptyIfNull (dataWorksWorkflowSpec .getComponents ()).stream ()
130+ .filter (x -> StringUtils .isBlank (functionId ) || StringUtils .equals (x .getId (), functionId ))
131+ .findFirst ()
132+ .map (specCom -> {
133+ File fileCom = new File ();
134+ fileCom .setFileName (specCom .getName ());
135+ fileCom .setOwner (Optional .ofNullable (specCom .getMetadata ()).map (m -> (String )m .get ("owner" )).orElse (null ));
136+ fileCom .setFileTypeStr (Optional .ofNullable (specCom .getScript ()).map (SpecScript ::getRuntime ).map (SpecScriptRuntime ::getCommand )
137+ .orElse (null ));
138+ fileCom .setFileType (getScriptCommandTypeId (specCom .getScript ()));
139+ fileCom .setUseType (NodeUseType .COMPONENT .getValue ());
140+ fileCom .setContent (Optional .ofNullable (specCom .getScript ()).map (SpecScript ::getContent ).orElse (null ));
141+ return fileCom ;
142+ }).orElse (null );
143+ }
144+
102145 private static File functionSpecToFile (Specification <DataWorksWorkflowSpec > spec , String functionId ) {
103146 DataWorksWorkflowSpec dataWorksWorkflowSpec = spec .getSpec ();
104147 if (spec .getSpec () == null ) {
@@ -154,20 +197,26 @@ private static Integer getScriptCommandTypeId(SpecScript script) {
154197 }
155198
156199 public static FileDetail nodeSpecToFileDetail (Specification <DataWorksWorkflowSpec > spec , String nodeId ) {
200+ return nodeSpecToFileDetail (spec , nodeId , null );
201+ }
202+
203+ public static FileDetail nodeSpecToFileDetail (Specification <DataWorksWorkflowSpec > spec , String nodeId , String content ) {
157204 FileDetail fileDetail = new FileDetail ();
158- fileDetail .setFile (nodeSpecToFile (spec , nodeId ));
205+ fileDetail .setFile (nodeSpecToFile (spec , nodeId , content ));
159206 fileDetail .setNodeCfg (nodeSpecToNodeCfg (spec , nodeId ));
160207 return fileDetail ;
161208 }
162209
163210 public static FileDetail nodeSpecToFileDetail (Specification <DataWorksWorkflowSpec > spec ) {
164211 FileDetail fileDetail = new FileDetail ();
165- fileDetail .setFile (nodeSpecToFile (spec , null ));
212+ String nodeId = Optional .ofNullable (MapUtils .emptyIfNull (spec .getMetadata ()).get ("uuid" ))
213+ .map (String ::valueOf ).orElse (null );
214+ fileDetail .setFile (nodeSpecToFile (spec , nodeId ));
166215 fileDetail .setNodeCfg (nodeSpecToNodeCfg (spec , null ));
167216 return fileDetail ;
168217 }
169218
170- public static File nodeSpecToFile (Specification <DataWorksWorkflowSpec > spec , String nodeId ) {
219+ public static File nodeSpecToFile (Specification <DataWorksWorkflowSpec > spec , String nodeId , String content ) {
171220 DataWorksWorkflowSpec dataWorksWorkflowSpec = spec .getSpec ();
172221 if (spec .getSpec () == null ) {
173222 log .warn ("dataworks workflow spec is null" );
@@ -181,7 +230,8 @@ public static File nodeSpecToFile(Specification<DataWorksWorkflowSpec> spec, Str
181230 file .setCloudUuid (null );
182231 file .setCommitStatus (null );
183232 file .setConnName (Optional .ofNullable (specNode .getDatasource ()).map (SpecDatasource ::getName ).orElse (null ));
184- file .setContent (Optional .ofNullable (specNode .getScript ()).map (SpecScript ::getContent ).orElse (null ));
233+ Optional .ofNullable (content ).ifPresent (x -> Optional .ofNullable (specNode .getScript ()).ifPresent (s -> s .setContent (x )));
234+ file .setContent (new DataWorksNodeAdapter (spec , specNode , Context .builder ().deployToScheduler (true ).build ()).getCode ());
185235 file .setCreateTime (null );
186236 file .setCreateUser (null );
187237 file .setCurrentVersion (null );
@@ -251,18 +301,47 @@ public static File nodeSpecToFile(Specification<DataWorksWorkflowSpec> spec, Str
251301 }).orElse (null );
252302 }
253303
304+ public static File nodeSpecToFile (Specification <DataWorksWorkflowSpec > spec , String nodeId ) {
305+ return nodeSpecToFile (spec , nodeId , null );
306+ }
307+
254308 public static SpecNode getMatchSpecNode (DataWorksWorkflowSpec dataWorksWorkflowSpec , String nodeId ) {
255- for (SpecNode node : dataWorksWorkflowSpec .getNodes ()) {
309+ for (SpecNode node : ListUtils .emptyIfNull (dataWorksWorkflowSpec .getNodes ())) {
310+ // normal nodes
256311 if (StringUtils .isBlank (nodeId ) || StringUtils .equalsIgnoreCase (node .getId (), nodeId )) {
257312 return node ;
258313 }
314+
315+ // inner nodes of normal nodes
259316 for (SpecNode innerNode : node .getInnerNodes ()) {
260317 if (StringUtils .isBlank (nodeId ) || StringUtils .equalsIgnoreCase (innerNode .getId (), nodeId )) {
261318 return innerNode ;
262319 }
263320 }
264321 }
265- return null ;
322+
323+ // workflow inner node
324+ SpecNode node = ListUtils .emptyIfNull (dataWorksWorkflowSpec .getWorkflows ()).stream ()
325+ .map (wf -> ListUtils .emptyIfNull (wf .getNodes ()))
326+ .map (nodes -> nodes .stream ().filter (n -> StringUtils .equalsIgnoreCase (nodeId , n .getId ())).findAny ().orElse (null ))
327+ .filter (Objects ::nonNull )
328+ .findAny ()
329+ .orElse (null );
330+ if (node != null ) {
331+ return node ;
332+ }
333+
334+ // inner nodes of workflow inner node
335+ return ListUtils .emptyIfNull (dataWorksWorkflowSpec .getWorkflows ()).stream ()
336+ // workflow nodes
337+ .map (wf -> ListUtils .emptyIfNull (wf .getNodes ()))
338+ .flatMap (List ::stream )
339+ // inner nodes of workflow nodes
340+ .map (nodes -> ListUtils .emptyIfNull (nodes .getInnerNodes ()))
341+ .map (nodes -> nodes .stream ().filter (n -> StringUtils .equalsIgnoreCase (nodeId , n .getId ())).findAny ().orElse (null ))
342+ .filter (Objects ::nonNull )
343+ .findAny ()
344+ .orElse (null );
266345 }
267346
268347 /**
@@ -283,7 +362,11 @@ public static FileNodeCfg nodeSpecToNodeCfg(Specification<DataWorksWorkflowSpec>
283362 FileNodeCfg nodeCfg = new FileNodeCfg ();
284363 nodeCfg .setAppId (null );
285364 nodeCfg .setBaselineId (null );
286- nodeCfg .setCreateTime (null );
365+ Optional .ofNullable (specNode .getMetadata ())
366+ .map (x -> x .get ("createTime" ))
367+ .map (String ::valueOf )
368+ .map (DateUtils ::convertStringToDate )
369+ .ifPresent (nodeCfg ::setCreateTime );
287370 nodeCfg .setCreateUser (null );
288371 nodeCfg .setCronExpress (Optional .ofNullable (specNode .getTrigger ()).map (SpecTrigger ::getCron ).orElse (null ));
289372 nodeCfg .setCycleType (CronExpressUtil .parseCronToCycleType (nodeCfg .getCronExpress ()));
@@ -411,8 +494,9 @@ public static FileDetail snapshotContentToFileDetail(DataSnapshot snapshotDto) {
411494 .flatMap (snapshot -> Optional .ofNullable (DataSnapshotContent .of (snapshot .getContent ()))
412495 .map (content -> {
413496 Specification <DataWorksWorkflowSpec > specification = SpecUtil .parseToDomain (content .getSpec ());
414- FileDetail fileDetail = nodeSpecToFileDetail (specification );
415- Optional .ofNullable (fileDetail .getFile ()).ifPresent (file -> file .setContent (content .getContent ()));
497+ String nodeId = Optional .ofNullable (MapUtils .emptyIfNull (specification .getMetadata ()).get ("uuid" ))
498+ .map (String ::valueOf ).orElse (snapshot .getEntityUuid ());
499+ FileDetail fileDetail = nodeSpecToFileDetail (specification , nodeId , content .getContent ());
416500 return fileDetail ;
417501 }))
418502 .orElse (null );
0 commit comments