Authored by mali

资源位

package com.yohoufo.resource.service.impl.resource;
import com.alibaba.fastjson.JSONObject;
import com.yoho.service.model.resource.resource.SingleImageParsedResource;
import com.yohoufo.resource.helper.MakeUrlService;
import com.yohoufo.resource.service.IResourceParse;
import com.yohoufo.resource.service.Resource;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* 单张图片
*/
@Service
public class SingleImageResourceParse implements IResourceParse {
@javax.annotation.Resource
private MakeUrlService makeUrlService;
public SingleImageParsedResource parse(Resource resource) {
SingleImageParsedResource parsedResource = new SingleImageParsedResource();
String _id=resource.getId().toString();
parsedResource.setTemplateId(_id);
parsedResource.setTemplateIntro(resource.getData().getString("template_intro"));
parsedResource.setData(parseData(resource));
parsedResource.setBeginShowTime(resource.getData().getInteger("begin_show_time"));
parsedResource.setEndShowTime(resource.getData().getInteger("end_show_time"));
return parsedResource;
}
private List<SingleImageParsedResource.Data> parseData(Resource resource) {
JSONObject data = resource.getData().getJSONObject("data");
if (data == null) {
return Collections.emptyList();
}
List<SingleImageParsedResource.Data> list = new ArrayList<>();
for (Object obj : data.values()) {
if (obj instanceof JSONObject) {
JSONObject value = (JSONObject) obj;
SingleImageParsedResource.Data tmp = new SingleImageParsedResource.Data();
tmp.setTitle(value.getString("alt"));
tmp.setAlt(value.getString("alt"));
tmp.setSrc(value.getString("src"));
tmp.setUrl(makeUrlService.makeUrl(value.getJSONObject("url"), resource.getClientType()));
list.add(tmp);
}
}
return list;
}
}
\ No newline at end of file
... ...
package com.yohoufo.resource.service.impl.resource;
import com.alibaba.fastjson.JSONObject;
import com.yohoufo.resource.helper.MakeUrlService;
import com.yohoufo.resource.service.IResourceParse;
import com.yohoufo.resource.service.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.Serializable;
/**
* Created by oneway.wang on 2017/7/10.
*/
@Service
public class TwoPictureResourceParse implements IResourceParse {
private static final Logger logger = LoggerFactory.getLogger(TwoPictureResourceParse.class);
public static final String TEMPLATE_NAME="twoPicture";
@Autowired
private MakeUrlService makeUrlService;
@Override
public Serializable parse(Resource resource) {
try {
return parseData(resource);
} catch (Exception e) {
logger.warn("Parse TwoPicture resource fail, resource code [{}] exception: {}", resource.getCode(), e);
return resource.getData();
}
}
private JSONObject parseData(Resource resource) {
JSONObject returnJSON=new JSONObject();
//加入最基础的东西
returnJSON.put("template_id",resource.getId().toString());
returnJSON.put("template_intro",resource.getData().getString("template_intro"));
// 处理 data(充分利用原数据库中数据转成的JSONObject,只是遍历之后把URLObject替换成解析之后的数据)
JSONObject data = resource.getData().getJSONObject("data");
returnJSON.put("template_name",this.TEMPLATE_NAME);
// 处理 两张图的list
for (String index: data.getJSONObject("list").keySet()) {
makeUrl(data.getJSONObject("list").getJSONObject(index), resource.getClientType());
}
returnJSON.put("data",data);
return returnJSON;
}
private void makeUrl(JSONObject jsonElement, String clientType) {
JSONObject jsonUrl = jsonElement.getJSONObject("url");
String strUrl = makeUrlService.makeUrl(jsonUrl, clientType);
jsonElement.put("url", strUrl);
}
}
\ No newline at end of file
... ...