+ private static final String FAILURE_MSG_PREFIX = "Ingestion failed for ";
+ private static final String ROLLBACK_MSG_SUFFIX = ". Rolling back distribution.";
+
+ private static Logger logger = LoggerFactory.getInstance().getLogger(ModelArtifact.class.getName());
+
+ private String modelVerId;
+ private String modelInvariantId;
+ private Node modelVer;
+ private boolean firstVersionOfModel = false;
+
+ public ModelArtifact() {
+ super(ArtifactType.MODEL);
+ }
+
+ public String getModelVerId() {
+ return modelVerId;
+ }
+
+ public void setModelVerId(String modelVerId) {
+ this.modelVerId = modelVerId;
+ }
+
+ public String getModelInvariantId() {
+ return modelInvariantId;
+ }
+
+ public void setModelInvariantId(String modelInvariantId) {
+ this.modelInvariantId = modelInvariantId;
+ }
+
+ public Node getModelVer() {
+ return modelVer;
+ }
+
+ public void setModelVer(Node modelVer) {
+ this.modelVer = modelVer;
+ }
+
+ @Override
+ public String getUniqueIdentifier() {
+ return getModelInvariantId() + "|" + getModelVerId();
+ }
+
+
+ /**
+ * Test whether the specified resource (URL) can be requested successfully
+ *
+ * @param aaiClient
+ * @param distId
+ * @param xmlResourceUrl
+ * @return true if a request to GET this resource as XML media is successful (status OK)
+ */
+ private boolean xmlResourceCanBeFetched(AaiRestClient aaiClient, String distId, String xmlResourceUrl) {
+ OperationResult getResponse = aaiClient.getResource(xmlResourceUrl, distId, MediaType.APPLICATION_XML_TYPE);
+ return getResponse != null && getResponse.getResultCode() == Response.Status.OK.getStatusCode();
+ }
+
+ /**
+ * PUT the specified XML resource
+ *
+ * @param aaiClient
+ * @param distId
+ * @param resourceUrl
+ * @param payload
+ * @return true if the resource PUT as XML media was successful (status OK)
+ */
+ private boolean putXmlResource(AaiRestClient aaiClient, String distId, String resourceUrl, String payload) {
+ OperationResult putResponse =
+ aaiClient.putResource(resourceUrl, payload, distId, MediaType.APPLICATION_XML_TYPE);
+ return putResponse != null && putResponse.getResultCode() == Response.Status.CREATED.getStatusCode();
+ }
+
+ @Override
+ public boolean push(AaiRestClient aaiClient, ModelLoaderConfig config, String distId,
+ List<Artifact> completedArtifacts) {
+ boolean success;
+
+ // See whether the model is already present
+ String resourceUrl = getModelUrl(config);
+
+ if (xmlResourceCanBeFetched(aaiClient, distId, resourceUrl)) {
+ logInfoMsg(getType().toString() + " " + getModelInvariantId() + " already exists. Skipping ingestion.");
+ success = pushModelVersion(aaiClient, config, distId, completedArtifacts);
+ } else {
+ // Assume that the model does not exist and attempt the PUT
+ success = putXmlResource(aaiClient, distId, resourceUrl, getPayload());
+ if (success) {
+ completedArtifacts.add(this);
+
+ // Record state to remember that this is the first version of the model (just added).
+ firstVersionOfModel = true;
+
+ logInfoMsg(getType().toString() + " " + getUniqueIdentifier() + " successfully ingested.");
+ } else {
+ logErrorMsg(
+ FAILURE_MSG_PREFIX + getType().toString() + " " + getUniqueIdentifier() + ROLLBACK_MSG_SUFFIX);
+ }