have done setting page
This commit is contained in:
@@ -44,6 +44,10 @@ public class CacheConfiguration {
|
||||
createCache(cm, com.sisvietnamvn.web.domain.User.class.getName());
|
||||
createCache(cm, com.sisvietnamvn.web.domain.Authority.class.getName());
|
||||
createCache(cm, com.sisvietnamvn.web.domain.User.class.getName() + ".authorities");
|
||||
createCache(cm, com.sisvietnamvn.web.domain.Media.class.getName());
|
||||
createCache(cm, com.sisvietnamvn.web.domain.Plugin.class.getName());
|
||||
createCache(cm, com.sisvietnamvn.web.domain.Setting.class.getName());
|
||||
createCache(cm, "settings");
|
||||
// jhipster-needle-ehcache-add-entry
|
||||
};
|
||||
}
|
||||
|
||||
+142
@@ -0,0 +1,142 @@
|
||||
package com.sisvietnamvn.web.controller.manage;
|
||||
|
||||
import com.sisvietnamvn.web.service.SettingService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/manage/settings")
|
||||
public class ManageSettingController {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ManageSettingController.class);
|
||||
|
||||
private final SettingService settingService;
|
||||
|
||||
public ManageSettingController(SettingService settingService) {
|
||||
this.settingService = settingService;
|
||||
}
|
||||
|
||||
private void addSettingsToModel(Model model) {
|
||||
model.addAttribute("settings", settingService.getAllAsMap());
|
||||
}
|
||||
|
||||
private String saveSettings(Map<String, String> allParams, RedirectAttributes redirectAttributes, String viewName) {
|
||||
// Remove CSRF token from params before saving
|
||||
allParams.remove("_csrf");
|
||||
|
||||
try {
|
||||
settingService.setValues(allParams);
|
||||
redirectAttributes.addFlashAttribute("successMessage", "Settings saved successfully.");
|
||||
} catch (Exception e) {
|
||||
LOG.error("Error saving settings", e);
|
||||
redirectAttributes.addFlashAttribute("errorMessage", "Failed to save settings: " + e.getMessage());
|
||||
}
|
||||
return "redirect:/manage/settings/" + viewName;
|
||||
}
|
||||
|
||||
// --- General Settings ---
|
||||
|
||||
@GetMapping("/general")
|
||||
public String showGeneralSettings(Model model) {
|
||||
addSettingsToModel(model);
|
||||
return "manage/settings/general";
|
||||
}
|
||||
|
||||
@PostMapping("/general/save")
|
||||
public String saveGeneralSettings(@RequestParam Map<String, String> allParams, RedirectAttributes redirectAttributes) {
|
||||
return saveSettings(allParams, redirectAttributes, "general");
|
||||
}
|
||||
|
||||
// --- Writing Settings ---
|
||||
|
||||
@GetMapping("/writing")
|
||||
public String showWritingSettings(Model model) {
|
||||
addSettingsToModel(model);
|
||||
return "manage/settings/writing";
|
||||
}
|
||||
|
||||
@PostMapping("/writing/save")
|
||||
public String saveWritingSettings(@RequestParam Map<String, String> allParams, RedirectAttributes redirectAttributes) {
|
||||
return saveSettings(allParams, redirectAttributes, "writing");
|
||||
}
|
||||
|
||||
// --- Reading Settings ---
|
||||
|
||||
@GetMapping("/reading")
|
||||
public String showReadingSettings(Model model) {
|
||||
addSettingsToModel(model);
|
||||
return "manage/settings/reading";
|
||||
}
|
||||
|
||||
@PostMapping("/reading/save")
|
||||
public String saveReadingSettings(@RequestParam Map<String, String> allParams, RedirectAttributes redirectAttributes) {
|
||||
// Handle un-checked checkboxes which don't send a value
|
||||
if (!allParams.containsKey("search_engine_visibility")) {
|
||||
allParams.put("search_engine_visibility", "0");
|
||||
}
|
||||
return saveSettings(allParams, redirectAttributes, "reading");
|
||||
}
|
||||
|
||||
// --- Discussion Settings ---
|
||||
|
||||
@GetMapping("/discussion")
|
||||
public String showDiscussionSettings(Model model) {
|
||||
addSettingsToModel(model);
|
||||
return "manage/settings/discussion";
|
||||
}
|
||||
|
||||
@PostMapping("/discussion/save")
|
||||
public String saveDiscussionSettings(@RequestParam Map<String, String> allParams, RedirectAttributes redirectAttributes) {
|
||||
// Handle checkboxes
|
||||
if (!allParams.containsKey("allow_comments")) allParams.put("allow_comments", "0");
|
||||
if (!allParams.containsKey("comment_author_must_fill")) allParams.put("comment_author_must_fill", "0");
|
||||
if (!allParams.containsKey("comment_manual_approval")) allParams.put("comment_manual_approval", "0");
|
||||
|
||||
return saveSettings(allParams, redirectAttributes, "discussion");
|
||||
}
|
||||
|
||||
// --- Media Settings ---
|
||||
|
||||
@GetMapping("/media")
|
||||
public String showMediaSettings(Model model) {
|
||||
addSettingsToModel(model);
|
||||
return "manage/settings/media";
|
||||
}
|
||||
|
||||
@PostMapping("/media/save")
|
||||
public String saveMediaSettings(@RequestParam Map<String, String> allParams, RedirectAttributes redirectAttributes) {
|
||||
return saveSettings(allParams, redirectAttributes, "media");
|
||||
}
|
||||
|
||||
// --- Permalinks Settings ---
|
||||
|
||||
@GetMapping("/permalinks")
|
||||
public String showPermalinksSettings(Model model) {
|
||||
addSettingsToModel(model);
|
||||
return "manage/settings/permalinks";
|
||||
}
|
||||
|
||||
@PostMapping("/permalinks/save")
|
||||
public String savePermalinksSettings(@RequestParam Map<String, String> allParams, RedirectAttributes redirectAttributes) {
|
||||
return saveSettings(allParams, redirectAttributes, "permalinks");
|
||||
}
|
||||
|
||||
// --- Privacy Settings ---
|
||||
|
||||
@GetMapping("/privacy")
|
||||
public String showPrivacySettings(Model model) {
|
||||
addSettingsToModel(model);
|
||||
return "manage/settings/privacy";
|
||||
}
|
||||
|
||||
@PostMapping("/privacy/save")
|
||||
public String savePrivacySettings(@RequestParam Map<String, String> allParams, RedirectAttributes redirectAttributes) {
|
||||
return saveSettings(allParams, redirectAttributes, "privacy");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package com.sisvietnamvn.web.domain;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import org.hibernate.annotations.Cache;
|
||||
import org.hibernate.annotations.CacheConcurrencyStrategy;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* A Setting.
|
||||
* This entity stores key-value configuration pairs for the application.
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "sis_setting")
|
||||
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
|
||||
public class Setting extends AbstractAuditingEntity<Long> implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
|
||||
@SequenceGenerator(name = "sequenceGenerator", sequenceName = "sis_setting_seq", allocationSize = 1)
|
||||
private Long id;
|
||||
|
||||
@NotNull
|
||||
@Size(max = 100)
|
||||
@Column(name = "setting_key", length = 100, nullable = false, unique = true)
|
||||
private String settingKey;
|
||||
|
||||
@Size(max = 2000)
|
||||
@Column(name = "setting_value", length = 2000)
|
||||
private String settingValue;
|
||||
|
||||
// jhipster-needle-entity-add-field - JHipster will add fields here
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Setting id(Long id) {
|
||||
this.setId(id);
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getSettingKey() {
|
||||
return settingKey;
|
||||
}
|
||||
|
||||
public void setSettingKey(String settingKey) {
|
||||
this.settingKey = settingKey;
|
||||
}
|
||||
|
||||
public Setting settingKey(String settingKey) {
|
||||
this.setSettingKey(settingKey);
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getSettingValue() {
|
||||
return settingValue;
|
||||
}
|
||||
|
||||
public void setSettingValue(String settingValue) {
|
||||
this.settingValue = settingValue;
|
||||
}
|
||||
|
||||
public Setting settingValue(String settingValue) {
|
||||
this.setSettingValue(settingValue);
|
||||
return this;
|
||||
}
|
||||
|
||||
// jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (!(o instanceof Setting)) {
|
||||
return false;
|
||||
}
|
||||
return id != null && id.equals(((Setting) o).id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return getClass().hashCode();
|
||||
}
|
||||
|
||||
// prettier-ignore
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Setting{" +
|
||||
"id=" + getId() +
|
||||
", settingKey='" + getSettingKey() + "'" +
|
||||
", settingValue='" + getSettingValue() + "'" +
|
||||
"}";
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.sisvietnamvn.web.repository;
|
||||
|
||||
import com.sisvietnamvn.web.domain.Setting;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Spring Data JPA repository for the Setting entity.
|
||||
*/
|
||||
@Repository
|
||||
public interface SettingRepository extends JpaRepository<Setting, Long> {
|
||||
Optional<Setting> findBySettingKey(String settingKey);
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package com.sisvietnamvn.web.service;
|
||||
|
||||
import com.sisvietnamvn.web.domain.Setting;
|
||||
import com.sisvietnamvn.web.repository.SettingRepository;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class SettingService {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(SettingService.class);
|
||||
|
||||
private final SettingRepository settingRepository;
|
||||
|
||||
public SettingService(SettingRepository settingRepository) {
|
||||
this.settingRepository = settingRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a setting value by key.
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
@Cacheable(value = "settings", key = "#key")
|
||||
public String getValue(String key, String defaultValue) {
|
||||
return settingRepository.findBySettingKey(key)
|
||||
.map(Setting::getSettingValue)
|
||||
.orElse(defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a setting value by key.
|
||||
*/
|
||||
@CacheEvict(value = "settings", allEntries = true)
|
||||
public void setValue(String key, String value) {
|
||||
Optional<Setting> settingOpt = settingRepository.findBySettingKey(key);
|
||||
if (settingOpt.isPresent()) {
|
||||
Setting setting = settingOpt.get();
|
||||
setting.setSettingValue(value);
|
||||
settingRepository.save(setting);
|
||||
LOG.debug("Updated setting '{}' to '{}'", key, value);
|
||||
} else {
|
||||
Setting setting = new Setting();
|
||||
setting.setSettingKey(key);
|
||||
setting.setSettingValue(value);
|
||||
settingRepository.save(setting);
|
||||
LOG.debug("Created new setting '{}' with value '{}'", key, value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set multiple settings at once.
|
||||
*/
|
||||
@CacheEvict(value = "settings", allEntries = true)
|
||||
public void setValues(Map<String, String> settings) {
|
||||
settings.forEach(this::setValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all settings as a map for easy rendering in UI templates.
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
public Map<String, String> getAllAsMap() {
|
||||
List<Setting> allSettings = settingRepository.findAll();
|
||||
return allSettings.stream()
|
||||
.collect(Collectors.toMap(Setting::getSettingKey, setting ->
|
||||
setting.getSettingValue() == null ? "" : setting.getSettingValue()
|
||||
));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user