Commit 2f91496c authored by mainlove's avatar mainlove

plugin的通用設置功能 需要找個在線編輯json的插件

parent e177a9de
......@@ -26,7 +26,6 @@ import javax.servlet.http.HttpServletResponse;
import org.b3log.latke.Keys;
import org.b3log.latke.Latkes;
import org.b3log.latke.model.Plugin;
import org.b3log.latke.plugin.AbstractPlugin;
import org.b3log.latke.service.LangPropsService;
import org.b3log.latke.servlet.HTTPRequestContext;
import org.b3log.latke.servlet.HTTPRequestMethod;
......@@ -197,14 +196,18 @@ public final class PluginConsole {
try {
final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, response);
final String pluginoId = requestJSONObject.getString(Keys.OBJECT_ID);
final String pluginId = requestJSONObject.getString(Keys.OBJECT_ID);
final AbstractPlugin result = pluginQueryService.getPlugin(pluginoId);
final String setting = pluginQueryService.getPluginSetting(pluginId);
renderer.setTemplateName("admin-plugin-setting.ftl");
final Map<String, Object> dataModel = renderer.getDataModel();
dataModel.put(Plugin.PLUGIN, result);
Keys.fillServer(dataModel);
Keys.fillRuntime(dataModel);
dataModel.put(Plugin.PLUGIN_SETTING, setting);
dataModel.put(Keys.OBJECT_ID, pluginId);
} catch (final Exception e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
......@@ -218,4 +221,29 @@ public final class PluginConsole {
}
/**
* update the setting of the plugin.
*
* @param request the specified http servlet request
* @param response the specified http servlet response
* @param context the specified http request context
* @param renderer the specified {@link ConsoleRenderer}
* @throws Exception exception
*/
@RequestProcessing(value = "/console/plugin/updateSetting", method = HTTPRequestMethod.POST)
public void updateSetting(final HttpServletRequest request, final HttpServletResponse response, final HTTPRequestContext context,
final JSONRenderer renderer) throws Exception {
context.setRenderer(renderer);
final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, response);
final String pluginoId = requestJSONObject.getString(Keys.OBJECT_ID);
final String settings = requestJSONObject.getString(Plugin.PLUGIN_SETTING);
final JSONObject ret = pluginMgmtService.updatePluginSetting(pluginoId, settings);
renderer.setJSONObject(ret);
}
}
......@@ -23,6 +23,6 @@ import org.b3log.latke.repository.Repository;
* Plugin repository.
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.0, Jul 21, 2011
* @version 1.0.0.0, Jan 24, 2013
*/
public interface PluginRepository extends Repository {}
......@@ -17,6 +17,7 @@ package org.b3log.solo.repository.impl;
import java.util.logging.Logger;
import org.b3log.latke.model.Plugin;
import org.b3log.latke.repository.AbstractRepository;
import org.b3log.solo.repository.PluginRepository;
......@@ -58,4 +59,5 @@ public final class PluginRepositoryImpl extends AbstractRepository implements Pl
private PluginRepositoryImpl(final String name) {
super(name);
}
}
......@@ -20,8 +20,10 @@ import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.b3log.latke.Keys;
import org.b3log.latke.Latkes;
import org.b3log.latke.model.Plugin;
import org.b3log.latke.plugin.AbstractPlugin;
import org.b3log.latke.plugin.PluginManager;
import org.b3log.latke.plugin.PluginStatus;
......@@ -115,6 +117,58 @@ public final class PluginMgmtService {
return ret;
}
/**
* updatePluginSetting.
*
* @param pluginId the specified pluginoId
* @param setting the specified setting
* @return the ret json
*/
public JSONObject updatePluginSetting(final String pluginId, final String setting) {
final Map<String, String> langs = langPropsService.getAll(Latkes.getLocale());
final PluginManager pluginManager = PluginManager.getInstance();
final List<AbstractPlugin> plugins = pluginManager.getPlugins();
final JSONObject ret = new JSONObject();
for (final AbstractPlugin plugin : plugins) {
if (plugin.getId().equals(pluginId)) {
final Transaction transaction = pluginRepository.beginTransaction();
try {
final JSONObject pluginJson = plugin.toJSONObject();
pluginJson.put(Plugin.PLUGIN_SETTING, setting);
pluginRepository.update(pluginId, pluginJson);
transaction.commit();
ret.put(Keys.STATUS_CODE, true);
ret.put(Keys.MSG, langs.get("setSuccLabel"));
return ret;
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.SEVERE, "Set plugin status error", e);
ret.put(Keys.STATUS_CODE, false);
ret.put(Keys.MSG, langs.get("setFailLabel"));
return ret;
}
}
}
ret.put(Keys.STATUS_CODE, false);
ret.put(Keys.MSG, langs.get("refreshAndRetryLabel"));
return ret;
}
/**
* Gets the {@link PluginMgmtService} singleton.
*
......@@ -147,4 +201,5 @@ public final class PluginMgmtService {
*/
private SingletonHolder() {}
}
}
......@@ -20,14 +20,17 @@ import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.b3log.latke.model.Pagination;
import org.b3log.latke.model.Plugin;
import org.b3log.latke.plugin.AbstractPlugin;
import org.b3log.latke.plugin.PluginManager;
import org.b3log.latke.repository.RepositoryException;
import org.b3log.latke.service.ServiceException;
import org.b3log.latke.util.Paginator;
import org.b3log.solo.repository.PluginRepository;
import org.b3log.solo.repository.impl.PluginRepositoryImpl;
import org.json.JSONException;
import org.json.JSONObject;
......@@ -121,28 +124,31 @@ public final class PluginQueryService {
}
/**
* get the {@link AbstractPlugin} by the specified pluginoId.
* get the setting(json formatter) of the plugin(from database not cache which does not contains it) by the specified pluginoId.
*
* @param pluginoId the specified pluginId
* @param pluginId the specified pluginId
* @return the {@link AbstractPlugin}
* @throws ServiceException service exception
* @throws JSONException json exception
*/
public AbstractPlugin getPlugin(final String pluginoId) throws ServiceException {
public String getPluginSetting(final String pluginId) throws ServiceException, JSONException {
final List<AbstractPlugin> plugins = PluginManager.getInstance().getPlugins();
AbstractPlugin ret = null;
JSONObject ret = null;
try {
ret = pluginRepository.get(pluginId);
} catch (final RepositoryException e) {
LOGGER.log(Level.SEVERE, "get plugin[" + pluginId + "] fail");
throw new ServiceException("get plugin[" + pluginId + "] fail");
for (final AbstractPlugin plugin : plugins) {
if (plugin.getId().equals(pluginoId)) {
ret = plugin;
}
}
if (ret == null) {
LOGGER.log(Level.SEVERE, "can not find plugin[" + pluginoId + "]");
throw new ServiceException("can not find plugin[" + pluginoId + "]");
LOGGER.log(Level.SEVERE, "can not find plugin[" + pluginId + "]");
throw new ServiceException("can not find plugin[" + pluginId + "]");
}
return ret;
return ret.optString(Plugin.PLUGIN_SETTING).toString();
}
/**
......
......@@ -390,4 +390,5 @@
<li><a href="http://code.google.com/p/b3log-solo/issues/detail?id=2">2 Tree-House 皮肤评论预览有问题</a></li>
</ul>
</body>
</html>
kkkk
\ No newline at end of file
<textarea id="jsoneditor" rows="10" cols="110">
${setting}
</textarea>
<input type="hidden" id="pluginId" value="${oId}">
<button class="marginRight12" id="updateSetting" onclick="updateSetting()">save</button>
<script type="text/javascript">
function updateSetting(){
var pluginId = $("#pluginId").val();
var json = $("#jsoneditor").val();
alert(json);
$("#loadMsg").text(Label.loadingLabel);
var requestJSONObject = {
"oId": pluginId,
"setting":json
};
$.ajax({
url: latkeConfig.servePath + "/console/plugin/updateSetting",
type: "POST",
cache: false,
data: JSON.stringify(requestJSONObject),
success: function(result, textStatus){
$("#tipMsg").text(result.msg);
}
});
}
</script>
......@@ -120,18 +120,16 @@ admin.pluginList = {
$("#PluginSetting").html(result);
$("#PluginSetting").dialog({
width: 700,
height: 190,
height: 400,
"modal": true,
"hideFooter": true
});
$("#PluginSetting").dialog("open");
$("#loadMsg").text("");
}
});
},
changeStatus: function (pluginId, status) {
if (status === "ENABLED") {
status = "DISABLED";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment