/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sdm.core.resource;

import java.util.HashMap;

import javax.annotation.security.PermitAll;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.sdm.core.Setting;
import com.sdm.core.exception.InvalidRequestException;
import com.sdm.core.response.DefaultResponse;
import com.sdm.core.response.IBaseResponse;
import com.sdm.core.response.model.MessageModel;

/**
 *
 * @author Htoonlin
 */
@Path("/")
public class SystemResource extends DefaultResource {

	@PermitAll
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public IBaseResponse welcome() throws Exception {
		DefaultResponse response = this.validateCache();
		if (response != null) {
			return response;
		}
		response = new DefaultResponse(new MessageModel(200, "Welcome!", "Never give up to be a warrior."));
		response.setHeaders(this.buildCache());
		return response;
	}
	
	@GET
	@Path("setting")
	@Produces(MediaType.APPLICATION_JSON)
	public IBaseResponse getAllSetting() {
		DefaultResponse response = this.validateCache();
		// Cache validation
		if (response != null) {
			return response;
		}
		response = new DefaultResponse<>(Setting.getInstance().getProperties());
		response.setHeaders(this.buildCache());
		return response;
	}

	@POST
	@Path("setting")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public IBaseResponse updateAllSetting(HashMap<String, String> request) {
		InvalidRequestException invalidRequest = new InvalidRequestException();
		for (String key : request.keySet()) {
			String value = request.get(key);
			if (key.toLowerCase().startsWith("com.sdm.path")) {
				invalidRequest.addError(key, "Can't modified this property <" + key + ">", value);
				continue;
			}
			Setting.getInstance().changeSetting(key.toLowerCase(), value);
		}
		if (invalidRequest.getErrors().size() > 0) {
			throw invalidRequest;
		}

		Setting.getInstance().save();
		this.modifiedResource();

		return new DefaultResponse<>(Setting.getInstance().getProperties());
	}
}

@@ -636,32 +636,76 @@
 IBaseResponse;%0D%0A
+import com.sdm.core.response.ResponseType;%0D%0A
 import com.sdm.c
@@ -738,16 +738,16 @@
 Model;%0D%0A
-
 %0D%0A/**%0D%0A 
@@ -1253,24 +1253,25 @@
 ath(%22setting
+s
 %22)%0D%0A%09@Produc
@@ -1498,32 +1498,59 @@
 faultResponse%3C%3E(
+200, ResponseType.SUCCESS, 
 Setting.getInsta
@@ -1666,16 +1666,17 @@
 %22setting
+s
 %22)%0D%0A%09@Co
@@ -2360,16 +2360,16 @@
 e();%0D%0A%0D%0A
-
 %09%09return
@@ -2391,16 +2391,43 @@
 ponse%3C%3E(
+200, ResponseType.SUCCESS, 
 Setting.

Change Setting Path