-
Notifications
You must be signed in to change notification settings - Fork 588
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
269 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
griffin-metric/src/main/java/org/apache/griffin/metric/exception/GriffinErr.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.apache.griffin.metric.exception; | ||
|
||
import lombok.Getter; | ||
|
||
public enum GriffinErr { | ||
commonError(101, "Hit an error without details."), | ||
validationError(102, "Data validation fails due to [%s]"), | ||
// db operation errors | ||
dbInsertionError(301, "Fail to insert a record."), | ||
dbUpdateError(302, "Fail to update a record."), | ||
dbDeletionError(303, "Fail to delete a record."), | ||
; | ||
|
||
@Getter | ||
private int code; | ||
|
||
@Getter | ||
private String message; | ||
|
||
GriffinErr(int code, String message) { | ||
this.code = code; | ||
this.message = message; | ||
} | ||
|
||
public GriffinErrorEntity buildErrorEntity() { | ||
return new GriffinErrorEntity(this); | ||
} | ||
|
||
public GriffinErrorEntity buildErrorEntity(String message) { | ||
return new GriffinErrorEntity(this, message); | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
griffin-metric/src/main/java/org/apache/griffin/metric/exception/GriffinErrorEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.apache.griffin.metric.exception; | ||
|
||
import lombok.Data; | ||
import org.apache.griffin.metric.entity.BaseEntity; | ||
|
||
@Data | ||
public class GriffinErrorEntity extends BaseEntity { | ||
|
||
private Integer code; | ||
|
||
private String message; | ||
|
||
public GriffinErrorEntity(GriffinErr err) { | ||
this.code = err.getCode(); | ||
this.message = err.getMessage(); | ||
} | ||
|
||
public GriffinErrorEntity(GriffinErr err, String details) { | ||
this.code = err.getCode(); | ||
this.message = String.format(err.getMessage(), details); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
griffin-metric/src/main/java/org/apache/griffin/metric/exception/GriffinException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,28 @@ | ||
package org.apache.griffin.metric.exception; | ||
|
||
import lombok.Getter; | ||
|
||
public class GriffinException extends RuntimeException { | ||
@Getter | ||
private final GriffinErr error; | ||
|
||
public GriffinException(String message) { | ||
super(message); | ||
this.error = GriffinErr.commonError; | ||
} | ||
|
||
public GriffinException(Throwable cause) { | ||
super(cause); | ||
this.error = GriffinErr.commonError; | ||
} | ||
|
||
public GriffinException(String message, Throwable cause) { | ||
super(message, cause); | ||
this.error = GriffinErr.commonError; | ||
} | ||
|
||
public GriffinException(String message, GriffinErr error) { | ||
super(message); | ||
this.error = error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 0 additions & 38 deletions
38
griffin-metric/src/main/java/org/apache/griffin/metric/service/MetricTagDService.java
This file was deleted.
Oops, something went wrong.
111 changes: 111 additions & 0 deletions
111
griffin-metric/src/main/java/org/apache/griffin/metric/service/MetricTagService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package org.apache.griffin.metric.service; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.griffin.metric.dao.MetricTagDDao; | ||
import org.apache.griffin.metric.dao.TagAttachmentDao; | ||
import org.apache.griffin.metric.entity.BaseEntity; | ||
import org.apache.griffin.metric.entity.MetricTagD; | ||
import org.apache.griffin.metric.entity.TagAttachment; | ||
import org.apache.griffin.metric.exception.GriffinErr; | ||
import org.apache.griffin.metric.exception.GriffinException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@Slf4j | ||
public class MetricTagService { | ||
|
||
public static final String METRIC_TAG_D = "/metricTagD"; | ||
|
||
public static final String TAGS = "/tags"; | ||
|
||
private final MetricTagDDao metricTagDDao; | ||
|
||
private final TagAttachmentDao tagAttachmentDao; | ||
|
||
public MetricTagService(MetricTagDDao metricTagDDao, TagAttachmentDao tagAttachmentDao) { | ||
this.metricTagDDao = metricTagDDao; | ||
this.tagAttachmentDao = tagAttachmentDao; | ||
} | ||
|
||
@PutMapping(value = METRIC_TAG_D,consumes = MediaType.APPLICATION_JSON_VALUE, | ||
produces = MediaType.APPLICATION_JSON_VALUE) | ||
@ResponseStatus(HttpStatus.CREATED) | ||
public ResponseEntity<BaseEntity> createMetricTagD(@RequestBody MetricTagD metricTagD) { | ||
try { | ||
int id = metricTagDDao.addMetricTagD(metricTagD); | ||
if (id != 1) { | ||
return new ResponseEntity<>(GriffinErr.dbInsertionError.buildErrorEntity(), | ||
HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
return new ResponseEntity<>(metricTagD, HttpStatus.CREATED); | ||
} catch (GriffinException e) { | ||
return new ResponseEntity<>(e.getError().buildErrorEntity(e.getMessage()), | ||
HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
|
||
@PostMapping(value = METRIC_TAG_D, consumes = MediaType.APPLICATION_JSON_VALUE, | ||
produces = MediaType.APPLICATION_JSON_VALUE) | ||
@ResponseStatus(HttpStatus.ACCEPTED) | ||
public ResponseEntity<BaseEntity> updateMetricTagD(@RequestBody MetricTagD metricTagD) { | ||
boolean ret = metricTagDDao.updateById(metricTagD); | ||
return ret ? new ResponseEntity<>(metricTagD, HttpStatus.ACCEPTED) : new ResponseEntity<>( | ||
GriffinErr.dbUpdateError.buildErrorEntity(), | ||
HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
@DeleteMapping(value = METRIC_TAG_D + "/{id}") | ||
@ResponseStatus(HttpStatus.OK) | ||
public ResponseEntity<BaseEntity> deleteMetricTagD(@PathVariable @NonNull String id) { | ||
boolean ret = metricTagDDao.deleteById(id); | ||
return ret ? new ResponseEntity<>(HttpStatus.OK) : new ResponseEntity<>( | ||
GriffinErr.dbDeletionError.buildErrorEntity(), | ||
HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
@PutMapping(value = TAGS,consumes = MediaType.APPLICATION_JSON_VALUE, | ||
produces = MediaType.APPLICATION_JSON_VALUE) | ||
@ResponseStatus(HttpStatus.CREATED) | ||
public ResponseEntity<BaseEntity> attachTags(@RequestBody TagAttachment tagAttachment) { | ||
try { | ||
int id = tagAttachmentDao.addMetricTags(tagAttachment); | ||
if (id != 1) { | ||
return new ResponseEntity<>(GriffinErr.dbInsertionError.buildErrorEntity(), | ||
HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
return new ResponseEntity<>(tagAttachment, HttpStatus.CREATED); | ||
} catch (GriffinException e) { | ||
return new ResponseEntity<>(e.getError().buildErrorEntity(e.getMessage()), | ||
HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
|
||
@PostMapping(value = TAGS, consumes = MediaType.APPLICATION_JSON_VALUE, | ||
produces = MediaType.APPLICATION_JSON_VALUE) | ||
@ResponseStatus(HttpStatus.ACCEPTED) | ||
public ResponseEntity<BaseEntity> updateMetricD(@RequestBody TagAttachment tagAttachment) { | ||
boolean ret = tagAttachmentDao.updateById(tagAttachment); | ||
return ret ? new ResponseEntity<>(tagAttachment, HttpStatus.ACCEPTED) : new ResponseEntity<>( | ||
GriffinErr.dbUpdateError.buildErrorEntity(), | ||
HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
@DeleteMapping(value = TAGS + "/{id}") | ||
@ResponseStatus(HttpStatus.OK) | ||
public ResponseEntity<BaseEntity> deleteMetricD(@PathVariable @NonNull String id) { | ||
boolean ret = tagAttachmentDao.deleteById(id); | ||
return ret ? new ResponseEntity<>(HttpStatus.OK) : new ResponseEntity<>( | ||
GriffinErr.dbDeletionError.buildErrorEntity(), | ||
HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} |
Oops, something went wrong.