3535import static java .nio .charset .StandardCharsets .UTF_8 ;
3636import static java .util .Arrays .asList ;
3737import static java .util .Collections .emptyList ;
38+ import static java .util .Collections .singletonList ;
3839import static org .springframework .util .StringUtils .isEmpty ;
3940
4041/**
@@ -101,7 +102,7 @@ public FutureResult<Void> loadDataset(final Project project, final DatasetManife
101102 final ByteArrayInputStream inputStream = new ByteArrayInputStream (manifestJson .getBytes (UTF_8 ));
102103 dataStoreService .upload (dirPath .resolve (MANIFEST_FILE_NAME ).toString (), inputStream );
103104
104- return pullLoad (project , dirPath , manifest .getDataSet ());
105+ return pullLoad (project , dirPath , manifest .getDataSet (), false );
105106 } catch (IOException e ) {
106107 throw new DatasetException ("Unable to serialize manifest" , manifest .getDataSet (), e );
107108 } catch (DataStoreException | GoodDataRestException | RestClientException e ) {
@@ -156,7 +157,7 @@ public FutureResult<Void> loadDatasets(final Project project, final Collection<D
156157 final ByteArrayInputStream inputStream = new ByteArrayInputStream (manifestJson .getBytes (UTF_8 ));
157158 dataStoreService .upload (dirPath .resolve (MANIFEST_FILE_NAME ).toString (), inputStream );
158159
159- return pullLoad (project , dirPath , datasetsNames );
160+ return pullLoad (project , dirPath , datasetsNames , true );
160161 } catch (IOException e ) {
161162 throw new DatasetException ("Unable to serialize manifest" , datasetsNames , e );
162163 } catch (DataStoreException | GoodDataRestException | RestClientException e ) {
@@ -179,11 +180,11 @@ private void validateUploadManifests(final Collection<DatasetManifest> datasets)
179180 }
180181 }
181182
182- private FutureResult <Void > pullLoad (Project project , final Path dirPath , final String dataset ) {
183- return pullLoad (project , dirPath , asList (dataset ));
183+ private FutureResult <Void > pullLoad (Project project , final Path dirPath , final String dataset , boolean isBatchUpload ) {
184+ return pullLoad (project , dirPath , singletonList (dataset ), isBatchUpload );
184185 }
185186
186- private FutureResult <Void > pullLoad (Project project , final Path dirPath , final Collection <String > datasets ) {
187+ private FutureResult <Void > pullLoad (Project project , final Path dirPath , final Collection <String > datasets , final boolean isBatchUpload ) {
187188 final PullTask pullTask = restTemplate
188189 .postForObject (Pull .URI , new Pull (dirPath .toString ()), PullTask .class , project .getId ());
189190 return new PollResult <>(this , new SimplePollHandler <Void >(pullTask .getUri (), Void .class ) {
@@ -192,7 +193,7 @@ public boolean isFinished(ClientHttpResponse response) throws IOException {
192193 final PullTaskStatus status = extractData (response , PullTaskStatus .class );
193194 final boolean finished = status .isFinished ();
194195 if (finished && !status .isSuccess ()) {
195- final String message = getErrorMessage (status , dirPath );
196+ final String message = getErrorMessage (status , dirPath , isBatchUpload );
196197 throw new DatasetException (message , datasets );
197198 }
198199 return finished ;
@@ -215,37 +216,58 @@ protected void onFinish() {
215216
216217 }
217218
218- private String getErrorMessage (final PullTaskStatus status , final Path dirPath ) {
219+ private String getErrorMessage (final PullTaskStatus status , final Path dirPath , boolean isBatchUpload ) {
219220 String message = "status: " + status .getStatus ();
220221 try {
221- final FailStatus failStatus = download (dirPath .resolve (STATUS_FILE_NAME ), FailStatus .class );
222- if (failStatus != null ) {
223- final List <FailPart > errorParts = failStatus .getErrorParts ();
224- if (!errorParts .isEmpty ()) {
225- final List <String > errors = new ArrayList <>();
226- for (FailPart part : errorParts ) {
227- if (part .getLogName () != null ) {
228- try {
229- final String [] msg = download (dirPath .resolve (part .getLogName ()), String [].class );
230- errors .addAll (asList (msg ));
231- } catch (IOException | DataStoreException e ) {
232- if (part .getError () != null ) {
233- errors .add (part .getError ().getFormattedMessage ());
234- }
235- }
236- }
237- }
238- message = errors .toString ();
239- } else if (failStatus .getError () != null ) {
240- message = failStatus .getError ().getFormattedMessage ();
241- }
222+ Path statusFile = dirPath .resolve (STATUS_FILE_NAME );
223+ if (isBatchUpload ) {
224+ final BatchFailStatus batchFailStatus = download (statusFile , BatchFailStatus .class );
225+ message = getBatchFailStatusErrorMsg (dirPath , message , batchFailStatus );
226+ } else {
227+ final FailStatus failStatus = download (statusFile , FailStatus .class );
228+ message = getFailStatusErrorMsg (dirPath , message , failStatus );
242229 }
243230 } catch (IOException | DataStoreException ignored ) {
244231 // todo log?
245232 }
246233 return message ;
247234 }
248235
236+ private String getBatchFailStatusErrorMsg (final Path dirPath , final String defaultMessage , final BatchFailStatus batchFailStatus ) {
237+ if (batchFailStatus == null || batchFailStatus .getFailStatuses () == null ) {
238+ return defaultMessage ;
239+ }
240+ final List <String > messages = new ArrayList <>();
241+ for (FailStatus failStatus : batchFailStatus .getFailStatuses ()) {
242+ messages .add (getFailStatusErrorMsg (dirPath , defaultMessage , failStatus ));
243+ }
244+ return messages .isEmpty () ? defaultMessage : messages .toString ();
245+ }
246+
247+ private String getFailStatusErrorMsg (final Path dirPath , final String defaultMessage , final FailStatus failStatus ) {
248+ if (failStatus == null ) {
249+ return defaultMessage ;
250+ }
251+ final List <FailPart > errorParts = failStatus .getErrorParts ();
252+ if (errorParts .isEmpty ()){
253+ return (failStatus .getError () != null ) ? failStatus .getError ().getFormattedMessage () : defaultMessage ;
254+ }
255+ final List <String > errors = new ArrayList <>();
256+ for (FailPart part : errorParts ) {
257+ if (part .getLogName () != null ) {
258+ try {
259+ final String [] msg = download (dirPath .resolve (part .getLogName ()), String [].class );
260+ errors .addAll (asList (msg ));
261+ } catch (IOException | DataStoreException e ) {
262+ if (part .getError () != null ) {
263+ errors .add (part .getError ().getFormattedMessage ());
264+ }
265+ }
266+ }
267+ }
268+ return errors .toString ();
269+ }
270+
249271 private <T > T download (final Path path , final Class <T > type ) throws IOException {
250272 try (final InputStream input = dataStoreService .download (path .toString ())) {
251273 return mapper .readValue (input , type );
@@ -324,7 +346,6 @@ public void handlePollException(final GoodDataRestException e) {
324346 * @param project project to be updated
325347 * @param maqlDml update script to be executed in the project
326348 * @return poll result
327- *
328349 * @see com.gooddata.model.ModelService#updateProjectModel
329350 */
330351 public FutureResult <Void > updateProjectData (final Project project , final String maqlDml ) {
@@ -362,5 +383,4 @@ public void handlePollException(final GoodDataRestException e) {
362383 }
363384 });
364385 }
365-
366386}
0 commit comments