Showing
2 changed files
with
119 additions
and
0 deletions
resource/src/main/java/com/yohoufo/resource/service/impl/resource/SingleImageResourceParse.java
0 → 100644
1 | +package com.yohoufo.resource.service.impl.resource; | ||
2 | + | ||
3 | +import com.alibaba.fastjson.JSONObject; | ||
4 | +import com.yoho.service.model.resource.resource.SingleImageParsedResource; | ||
5 | +import com.yohoufo.resource.helper.MakeUrlService; | ||
6 | +import com.yohoufo.resource.service.IResourceParse; | ||
7 | +import com.yohoufo.resource.service.Resource; | ||
8 | +import org.springframework.stereotype.Service; | ||
9 | + | ||
10 | +import java.util.ArrayList; | ||
11 | +import java.util.Collections; | ||
12 | +import java.util.List; | ||
13 | + | ||
14 | +/** | ||
15 | + * 单张图片 | ||
16 | + */ | ||
17 | +@Service | ||
18 | +public class SingleImageResourceParse implements IResourceParse { | ||
19 | + | ||
20 | + @javax.annotation.Resource | ||
21 | + private MakeUrlService makeUrlService; | ||
22 | + | ||
23 | + public SingleImageParsedResource parse(Resource resource) { | ||
24 | + SingleImageParsedResource parsedResource = new SingleImageParsedResource(); | ||
25 | + String _id=resource.getId().toString(); | ||
26 | + parsedResource.setTemplateId(_id); | ||
27 | + parsedResource.setTemplateIntro(resource.getData().getString("template_intro")); | ||
28 | + parsedResource.setData(parseData(resource)); | ||
29 | + parsedResource.setBeginShowTime(resource.getData().getInteger("begin_show_time")); | ||
30 | + parsedResource.setEndShowTime(resource.getData().getInteger("end_show_time")); | ||
31 | + return parsedResource; | ||
32 | + } | ||
33 | + | ||
34 | + private List<SingleImageParsedResource.Data> parseData(Resource resource) { | ||
35 | + JSONObject data = resource.getData().getJSONObject("data"); | ||
36 | + if (data == null) { | ||
37 | + return Collections.emptyList(); | ||
38 | + } | ||
39 | + List<SingleImageParsedResource.Data> list = new ArrayList<>(); | ||
40 | + for (Object obj : data.values()) { | ||
41 | + if (obj instanceof JSONObject) { | ||
42 | + JSONObject value = (JSONObject) obj; | ||
43 | + SingleImageParsedResource.Data tmp = new SingleImageParsedResource.Data(); | ||
44 | + tmp.setTitle(value.getString("alt")); | ||
45 | + tmp.setAlt(value.getString("alt")); | ||
46 | + tmp.setSrc(value.getString("src")); | ||
47 | + tmp.setUrl(makeUrlService.makeUrl(value.getJSONObject("url"), resource.getClientType())); | ||
48 | + list.add(tmp); | ||
49 | + } | ||
50 | + | ||
51 | + } | ||
52 | + return list; | ||
53 | + } | ||
54 | + | ||
55 | +} |
resource/src/main/java/com/yohoufo/resource/service/impl/resource/TwoPictureResourceParse.java
0 → 100644
1 | +package com.yohoufo.resource.service.impl.resource; | ||
2 | + | ||
3 | +import com.alibaba.fastjson.JSONObject; | ||
4 | +import com.yohoufo.resource.helper.MakeUrlService; | ||
5 | +import com.yohoufo.resource.service.IResourceParse; | ||
6 | +import com.yohoufo.resource.service.Resource; | ||
7 | +import org.slf4j.Logger; | ||
8 | +import org.slf4j.LoggerFactory; | ||
9 | +import org.springframework.beans.factory.annotation.Autowired; | ||
10 | +import org.springframework.stereotype.Service; | ||
11 | + | ||
12 | +import java.io.Serializable; | ||
13 | + | ||
14 | +/** | ||
15 | + * Created by oneway.wang on 2017/7/10. | ||
16 | + */ | ||
17 | +@Service | ||
18 | +public class TwoPictureResourceParse implements IResourceParse { | ||
19 | + | ||
20 | + private static final Logger logger = LoggerFactory.getLogger(TwoPictureResourceParse.class); | ||
21 | + | ||
22 | + public static final String TEMPLATE_NAME="twoPicture"; | ||
23 | + @Autowired | ||
24 | + private MakeUrlService makeUrlService; | ||
25 | + | ||
26 | + @Override | ||
27 | + public Serializable parse(Resource resource) { | ||
28 | + try { | ||
29 | + return parseData(resource); | ||
30 | + } catch (Exception e) { | ||
31 | + logger.warn("Parse TwoPicture resource fail, resource code [{}] exception: {}", resource.getCode(), e); | ||
32 | + return resource.getData(); | ||
33 | + } | ||
34 | + } | ||
35 | + | ||
36 | + | ||
37 | + private JSONObject parseData(Resource resource) { | ||
38 | + | ||
39 | + JSONObject returnJSON=new JSONObject(); | ||
40 | + //加入最基础的东西 | ||
41 | + returnJSON.put("template_id",resource.getId().toString()); | ||
42 | + | ||
43 | + returnJSON.put("template_intro",resource.getData().getString("template_intro")); | ||
44 | + | ||
45 | + // 处理 data(充分利用原数据库中数据转成的JSONObject,只是遍历之后把URLObject替换成解析之后的数据) | ||
46 | + JSONObject data = resource.getData().getJSONObject("data"); | ||
47 | + | ||
48 | + | ||
49 | + returnJSON.put("template_name",this.TEMPLATE_NAME); | ||
50 | + // 处理 两张图的list | ||
51 | + for (String index: data.getJSONObject("list").keySet()) { | ||
52 | + makeUrl(data.getJSONObject("list").getJSONObject(index), resource.getClientType()); | ||
53 | + } | ||
54 | + returnJSON.put("data",data); | ||
55 | + return returnJSON; | ||
56 | + } | ||
57 | + | ||
58 | + private void makeUrl(JSONObject jsonElement, String clientType) { | ||
59 | + JSONObject jsonUrl = jsonElement.getJSONObject("url"); | ||
60 | + String strUrl = makeUrlService.makeUrl(jsonUrl, clientType); | ||
61 | + jsonElement.put("url", strUrl); | ||
62 | + } | ||
63 | + | ||
64 | +} |
-
Please register or login to post a comment