Skip to content

Commit

Permalink
新闻和栏目支持多国语言
Browse files Browse the repository at this point in the history
  • Loading branch information
ysc committed Oct 18, 2012
1 parent d0c4d28 commit 290f31a
Show file tree
Hide file tree
Showing 14 changed files with 666 additions and 110 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.apdplat.module.info.action;

import com.apdplat.module.info.model.InfoType;
import com.apdplat.module.info.model.News;
import com.apdplat.module.info.service.InfoTypeService;
import com.apdplat.platform.action.ExtJSSimpleAction;
import com.apdplat.platform.criteria.Property;
import com.apdplat.platform.util.Struts2Utils;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.context.annotation.Scope;
Expand All @@ -13,32 +17,76 @@
@Controller
@Namespace("/info")
public class InfoTypeAction extends ExtJSSimpleAction<InfoType> {
private String node;
@Resource(name="infoTypeService")
private InfoTypeService infoTypeService;

public String store(){
return query();
}
@Override
public String query(){
//如果node为null则采用普通查询方式
if(node==null){
return super.query();
}
//如果指定了node则采用自定义的查询方式
if("root".equals(node.trim())){
String json=infoTypeService.toRootJson();
Struts2Utils.renderJson(json);
}else{
int id=Integer.parseInt(node.trim());
String json=infoTypeService.toJson(id);
Struts2Utils.renderJson(json);
private String node;
@Resource(name = "infoTypeService")
private InfoTypeService infoTypeService;
private String lang = "zh";

//setInfoTypeName依赖于setLang,所以在创建的时候没有办法确保顺序
//所以要强制指定
@Override
protected void assemblyModelForCreate(InfoType model) {
model.forceSpecifyLanguageForCreate(lang);
}
@Override
protected void assemblyModelForPartUpdate(List<Property> properties) {
for(Property property : properties){
if("lang".equals(property.getName())){
properties.remove(property);
}
return null;
}
}
//修改模型的时候,在修改内容之前先设置语言
@Override
protected void old(InfoType model) {
log.info("控制器设置语言:"+lang);
model.setLang(lang);
}

public String store() {
return query();
}

public void setNode(String node) {
this.node = node;
@Override
protected void afterRender(Map map, InfoType obj) {
retrieveAfterRender(map, obj);
}

@Override
protected void retrieveAfterRender(Map map, InfoType obj) {
if (obj.getParent() != null) {
obj.getParent().setLang(lang);
map.put("parent_infoTypeName", obj.getParent().getInfoTypeName());
}
obj.setLang(lang);
map.put("infoTypeName", obj.getInfoTypeName());
map.remove("parent");
}

@Override
public String query() {
//如果node为null则采用普通查询方式
if (node == null) {
return super.query();
}
//如果指定了node则采用自定义的查询方式
if ("root".equals(node.trim())) {
String json = infoTypeService.toRootJson(lang);
Struts2Utils.renderJson(json);
} else {
int id = Integer.parseInt(node.trim());
String json = infoTypeService.toJson(id, lang);
Struts2Utils.renderJson(json);
}
return null;
}

public void setNode(String node) {
this.node = node;
}

public void setLang(String lang) {
this.lang = lang;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.apdplat.platform.action.ExtJSSimpleAction;
import com.apdplat.platform.action.converter.DateTypeConverter;
import com.apdplat.platform.criteria.Operator;
import com.apdplat.platform.criteria.Property;
import com.apdplat.platform.criteria.PropertyCriteria;
import com.apdplat.platform.criteria.PropertyEditor;
import com.apdplat.platform.criteria.PropertyType;
Expand All @@ -20,7 +21,28 @@
@Controller
@Namespace("/info")
public class NewsAction extends ExtJSSimpleAction<News> {
private String lang = "zh";
private int infoTypeId;

//setTitle和setContent依赖于setLang,所以在创建的时候没有办法确保顺序
//所以要强制指定
@Override
protected void assemblyModelForCreate(News model) {
model.forceSpecifyLanguageForCreate(lang);
}
@Override
protected void assemblyModelForPartUpdate(List<Property> properties) {
for(Property property : properties){
if("lang".equals(property.getName())){
properties.remove(property);
}
}
}
@Override
protected void old(News model) {
log.info("控制器设置语言:"+lang);
model.setLang(lang);
}
//方式二:使用IN语句
@Override
public PropertyCriteria buildPropertyCriteria(){
Expand All @@ -42,21 +64,23 @@ public PropertyCriteria buildPropertyCriteria(){
}
@Override
protected void renderJsonForRetrieve(Map map) {
model.setLang(lang);
render(map,model);
map.put("infoTypeId", model.getInfoType().getId());
map.put("content", model.getContent());
}
@Override
protected void renderJsonForQuery(List result) {
for (News model : page.getModels()) {
Map map = new HashMap();
render(map,model);
for (News news : page.getModels()) {
Map temp = new HashMap();
render(temp,news);

result.add(map);
result.add(temp);
}
}
@Override
protected void render(Map map,News model){
model.setLang(lang);
map.put("id", model.getId());
map.put("version", model.getVersion());
map.put("title", model.getTitle());
Expand All @@ -67,8 +91,10 @@ protected void render(Map map,News model){
map.put("updateTime", DateTypeConverter.toDefaultDateTime(model.getUpdateTime()));
map.put("enabled", model.isEnabled()==true?"是":"否");
}

public void setInfoTypeId(int infoTypeId) {
this.infoTypeId = infoTypeId;
}
public void setLang(String lang) {
this.lang = lang;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.apdplat.module.info.model;

import com.apdplat.platform.generator.ActionGenerator;
import com.apdplat.platform.annotation.ModelAttr;
import com.apdplat.platform.annotation.ModelAttrRef;
import com.apdplat.platform.annotation.RenderIgnore;
import com.apdplat.platform.generator.ActionGenerator;
import com.apdplat.platform.model.Model;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -13,6 +12,7 @@
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
Expand All @@ -21,7 +21,6 @@
import javax.xml.bind.annotation.XmlType;
import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableComponent;
import org.compass.annotations.SearchableProperty;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

Expand All @@ -32,22 +31,68 @@
@XmlType(name = "InfoType")
@Searchable
public class InfoType extends Model{
@SearchableProperty
@ModelAttr("类别名称")
protected String infoTypeName;

@Transient
@ModelAttr("语言")
protected String lang="zh";
@ModelAttr("顺序号")
protected int orderNum;
@ManyToOne
@SearchableComponent
@ModelAttr("父类别")
@ModelAttrRef("infoTypeName")
protected InfoType parent;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "parent")
@OrderBy("orderNum ASC")
@RenderIgnore
@ModelAttr("子类别")
protected List<InfoType> child=new ArrayList<InfoType>();
protected List<InfoType> child=new ArrayList<>();

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "infoType")
@RenderIgnore
@ModelAttr("多语言内容")
protected List<InfoTypeContent> infoTypeContents=new ArrayList<>();

public void forceSpecifyLanguageForCreate(String language){
if(infoTypeContents.size()==1 && id==null){
infoTypeContents.get(0).setLang(Lang.valueOf(language));
}
}

public String getLang() {
return lang;
}

public void setLang(String lang) {
this.lang = lang;
}

public String getInfoTypeName(){
for(InfoTypeContent infoTypeContent : infoTypeContents){
if(infoTypeContent.getLang().getSymbol().equals(lang)){
return infoTypeContent.getInfoTypeName();
}
}
return null;
}
//setInfoTypeName方法依赖于setLang方法先执行
public void setInfoTypeName(String infoTypeName){
log.info("设置新闻类别名称");
log.info("模型语言:"+lang);
InfoTypeContent infoTypeContent = getInfoTypeContent();
infoTypeContent.setInfoTypeName(infoTypeName);
}
private InfoTypeContent getInfoTypeContent(){
for(InfoTypeContent infoTypeContent : infoTypeContents){
if(infoTypeContent.getLang().getSymbol().equals(lang)){
return infoTypeContent;
}
}
InfoTypeContent infoTypeContent = new InfoTypeContent();
infoTypeContent.setLang(Lang.valueOf(lang));
infoTypeContent.setInfoType(this);
infoTypeContents.add(infoTypeContent);
return infoTypeContent;
}

@XmlElementWrapper(name = "subInfoTypes")
@XmlElement(name = "infoType")
Expand All @@ -68,13 +113,18 @@ public void setParent(InfoType parent) {
this.parent = parent;
}

@XmlAttribute
public String getInfoTypeName() {
return infoTypeName;
@XmlElementWrapper(name = "infoTypeContents")
@XmlElement(name = "infoTypeContent")
public List<InfoTypeContent> getInfoTypeContents() {
return infoTypeContents;
}

public void addInfoTypeContent(InfoTypeContent infoTypeContent) {
this.infoTypeContents.add(infoTypeContent);
}

public void setInfoTypeName(String infoTypeName) {
this.infoTypeName = infoTypeName;
public void removeInfoTypeContent(InfoTypeContent infoTypeContent) {
this.infoTypeContents.remove(infoTypeContent);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.apdplat.module.info.model;

import com.apdplat.platform.annotation.ModelAttr;
import com.apdplat.platform.generator.ActionGenerator;
import com.apdplat.platform.model.Model;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.ManyToOne;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;
import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableProperty;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Entity
@Scope("prototype")
@Component
@XmlType(name = "InfoTypeContent")
@Searchable
public class InfoTypeContent extends Model{
@ManyToOne
@ModelAttr("新闻类别")
protected InfoType infoType;

@Enumerated(EnumType.STRING)
protected Lang lang;

@SearchableProperty
@ModelAttr("类别名称")
protected String infoTypeName;


@XmlAttribute
public Lang getLang() {
return lang;
}

public void setLang(Lang lang) {
this.lang = lang;
}

@XmlTransient
public InfoType getInfoType() {
return infoType;
}

public void setInfoType(InfoType infoType) {
this.infoType = infoType;
}

@XmlAttribute
public String getInfoTypeName() {
return infoTypeName;
}

public void setInfoTypeName(String infoTypeName) {
this.infoTypeName = infoTypeName;
}



@Override
public String getMetaData() {
return "新闻类别多语言内容";
}
public static void main(String[] args){
InfoTypeContent obj=new InfoTypeContent();
//生成Action
ActionGenerator.generate(obj.getClass());
}
}
Loading

0 comments on commit 290f31a

Please sign in to comment.