Skip to content

Commit

Permalink
DHFPROD-1741 Add a generic Process class (#1765)
Browse files Browse the repository at this point in the history
* Add a generic Process class

* Adds copyright info
  • Loading branch information
akshaysonvane authored and aebadirad committed Jan 22, 2019
1 parent 927e6c6 commit 82b5513
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2012-2019 MarkLogic Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.marklogic.hub.processes;

import com.fasterxml.jackson.databind.JsonNode;

public interface Process {

enum ProcessType {
INGEST("ingest"),
MAPPING("mapping"),
CUSTOM("custom");

private String type;

ProcessType(String type) {
this.type = type;
}

public static ProcessType getProcessType(String type) {
for (ProcessType processType : ProcessType.values()) {
if (processType.toString().equals(type)) {
return processType;
}
}
return null;
}

public String toString() {
return this.type;
}
}

/**
* Returns the name of the processes
*
* @return a processes name
*/
String getProcessName();

/**
* Sets the name of the processes
*
* @param processName - a processes name
*/
void setProcessName(String processName);

/**
* Returns the type of the Process
*
* @return - a processes type
*/
ProcessType getProcessType();

/**
* Sets the type of the processes
*
* @param processType - a processes type
*/
void setProcessType(ProcessType processType);

/**
* Serializes the mapping as a json string
*
* @return the serialized JSON string
*/
String serialize();

/**
* Deserializes a json response and applies it to this mapping
*
* @param json - the JsonNode you want deserialize
* @return this mapping
*/
Process deserialize(JsonNode json);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2012-2019 MarkLogic Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.marklogic.hub.processes;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.marklogic.hub.error.DataHubProjectException;

public class ProcessImpl implements Process {

private String processName;
private ProcessType processType;

public String getProcessName() {
return processName;
}

public void setProcessName(String processName) {
this.processName = processName;
}

public ProcessType getProcessType() {
return processType;
}

public void setProcessType(ProcessType processType) {
this.processType = processType;
}

@Override
public String serialize() {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.writeValueAsString(this);
}
catch (JsonProcessingException e) {
throw new DataHubProjectException("Unable to serialize processes object.");
}
}

@Override
public Process deserialize(JsonNode json) {
if (json.has("name")) {
setProcessName(json.get("name").asText());
}

if (json.has("type")) {
setProcessType(ProcessType.getProcessType(json.get("type").asText()));
}

return this;
}
}

0 comments on commit 82b5513

Please sign in to comment.