class TargetTypesServiceImpl implements TargetTypesService {

	@Override
	public String addTargetTypeDetails(final CreateUpdateTargetTypeDetailsRequest targetTypeDetailsRequest, final String userId) throws PacManException {
		try { 
			String dataSource = targetTypeDetailsRequest.getDataSource().toLowerCase().trim();
			String type = targetTypeDetailsRequest.getName().toLowerCase().trim().replaceAll(" ", "-");
			String indexName = dataSource+"_"+type;
			StringBuilder payLoad = new StringBuilder("{ \"mappings\": {");
			payLoad.append("\""+type+"\":{},\"issue_"+type+"\": { \"_parent\": {\"type\": \""+type+"\"}},\"recommendation_"+type+"\": { \"_parent\": {\"type\": \""+type+"\"}},\"issue_"+type+"_audit\": { \"_parent\": {\"type\": \"issue_"+type+"\"}},\"issue_"+type+"_comment\": { \"_parent\": {\"type\": \"issue_"+type+"\"}},\"issue_"+type+"_exception\": { \"_parent\": {\"type\": \"issue_"+type+"\"}}");
	        payLoad.append("}}");
	        if(!indexExists(indexName)) {
	        	Response indexResponse = invokeAPI("PUT", indexName, payLoad.toString());
		        Response aliasResponse = invokeAPI("PUT", "/"+indexName+"/_alias/"+dataSource,null);
				if ((aliasResponse != null) && (indexResponse != null)) {
					if ((indexResponse.getStatusLine().getStatusCode() == 200) && (aliasResponse.getStatusLine().getStatusCode() == 200)) {
						return processTargetTypeCreation(targetTypeDetailsRequest, userId);
					} else {
						throw new PacManException(AdminConstants.TARGET_TYPE_CREATION_FAILURE);
					}
				} else {
					throw new PacManException(AdminConstants.TARGET_TYPE_CREATION_FAILURE);
				}
	        } else {
	        	throw new PacManException(AdminConstants.TARGET_TYPE_INDEX_EXITS);
	        }
		} catch(Exception exception) {
			log.error(UNEXPECTED_ERROR_OCCURRED, exception);
			throw new PacManException(AdminConstants.TARGET_TYPE_CREATION_FAILURE);
		}
	}

}