/*
* Copyright 2015-Present Entando Inc. (http://www.entando.com) All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.agiletec.plugins.jpwebdynamicform.aps.internalservlet.message;
import java.util.List;
import com.agiletec.aps.system.ApsSystemUtils;
import com.agiletec.aps.system.exception.ApsSystemException;
import com.agiletec.aps.system.services.i18n.II18nManager;
import com.agiletec.aps.system.services.lang.Lang;
import com.agiletec.aps.util.ApsProperties;
import com.agiletec.apsadmin.system.BaseAction;
import com.agiletec.plugins.jpwebdynamicform.aps.system.services.message.IMessageManager;
import com.agiletec.plugins.jpwebdynamicform.aps.system.services.message.model.SmallMessageType;
public class MessageTypesAction extends BaseAction {
public List<SmallMessageType> getMessageTypes() {
List<SmallMessageType> types = null;
try {
types = this.getMessageManager().getSmallMessageTypes();
this.checkTypeLables(types);
} catch (Throwable t) {
ApsSystemUtils.logThrowable(t, this, "getMessageTypes");
throw new RuntimeException("Error searching message types", t);
}
return types;
}
protected void checkTypeLables(List<SmallMessageType> types) {
if (null == types) {
return;
}
try {
for (int i = 0; i < types.size(); i++) {
SmallMessageType smallMessageType = types.get(i);
String labelKey = "jpwebdynamicform_TITLE_" + smallMessageType.getCode();
ApsProperties labelGroup = this.getI18nManager().getLabelGroup(labelKey);
if (null == labelGroup) {
this.addLabelGroups(labelKey, smallMessageType.getDescr());
}
String labelSubTitleKey = "jpwebdynamicform_SUBTITLE_" + smallMessageType.getCode();
ApsProperties labelSubTitleGroup = this.getI18nManager().getLabelGroup(labelSubTitleKey);
if (null == labelSubTitleGroup) {
this.addLabelGroups(labelSubTitleKey, smallMessageType.getDescr());
}
}
} catch (Throwable t) {
ApsSystemUtils.logThrowable(t, this, "checkTypeLables");
throw new RuntimeException("Error checking label types", t);
}
}
protected void addLabelGroups(String key, String defaultValue) throws ApsSystemException {
try {
ApsProperties properties = new ApsProperties();
Lang defaultLang = super.getLangManager().getDefaultLang();
properties.put(defaultLang.getCode(), defaultValue);
this.getI18nManager().addLabelGroup(key, properties);
} catch (Throwable t) {
ApsSystemUtils.logThrowable(t, this, "addLabelGroups");
throw new RuntimeException("Error adding label groups - key '" + key + "'", t);
}
}
protected II18nManager getI18nManager() {
return _i18nManager;
}
public void setI18nManager(II18nManager i18nManager) {
this._i18nManager = i18nManager;
}
protected IMessageManager getMessageManager() {
return _messageManager;
}
public void setMessageManager(IMessageManager messageManager) {
this._messageManager = messageManager;
}
private II18nManager _i18nManager;
private IMessageManager _messageManager;
}