重点技术-20170619-网站静态化-运行模板,批量页面静态化

//运行模板,批量页面静态化
@Service
@Transactional
public class MapperRunService
{
@Autowired
private PageDao pageDao;
@Autowired
private BaseDao<Mapper> baseDao;
public Properties execute(Long mapperId)
{
Properties properties = new MyProperties();
HttpFormGetSubmit httpFormGetSubmit = new HttpFormGetSubmit();
JsonUtil jsonUtil = JsonUtil.getInstance();
FreemarkerStaticUtil freemarkerStaticUtil = FreemarkerStaticUtil.getInstance();
if(mapperId == null || mapperId < 1)
{
return MyBackInfo.fail(properties, "请输入模板ID");
}
Mapper mapper = (Mapper) baseDao.findById(Mapper.class, mapperId, S_State.Normal);
if(mapper == null)
{
return MyBackInfo.fail(properties, "该模板不存在");
}
//文件上传到云存储的路径规范不定,参考如下:
//http://test.qiniu.com/NewsDetail/{tableId}.htm
//http://test.qiniu.com/NewsDetail_{tableId}.htm
//http://test.qiniu.com/NewsList/{pageNumber}/{countPerPage}.htm
//http://test.qiniu.com/NewsList_{pageNumber}_{countPerPage}.htm
//http://test.qiniu.com/NewsList/{pageNumber}/{countPerPage}.htm
//http://test.qiniu.com/NewsList.htm
try
{
//Step1:遍历   jsonParamStr 得到所有输入参数键值对
// 例如:
// tableId:"http://localhost/TemplateStatic/saIdList.sahtml?interfaceVersion=19000101&memberId=1"
JsonObject jsonObject = jsonUtil.jsonStrToJsonObj(mapper.getJsonParamStr());
Map<String, JsonArray> orgInputMap = new HashMap<String, JsonArray>();
for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet())
{
if(entry.getValue() == null) continue;
if(entry.getValue().isJsonArray())
{
//参数的值是一个数组,可以直接使用
orgInputMap.put(entry.getKey(), entry.getValue().getAsJsonArray());
}
else
{
//参数的值是一个请求地址,从该地址获取最新动态数据,返回结果里有idList,是一个数组
JsonObject responseObject = jsonUtil.jsonStrToJsonObj(httpFormGetSubmit.submitForm(entry.getValue().getAsString(), null));
if(S_NormalFlag.success.equals(responseObject.get(S_NormalFlag.result).getAsString()))
{
orgInputMap.put(entry.getKey(), responseObject.getAsJsonArray("idList"));
}
else
{
return MyBackInfo.fail(properties, responseObject.get(S_NormalFlag.info).getAsString());
}
}
}
// 最终全组合列表
List<Map<String, Object>> allCombinationList = new ArrayList<Map<String, Object>>();
new CombinationGeneratorUtil().execute(allCombinationList, new HashMap<String, Object>(), orgInputMap);
for (Map<String, Object> currentInputMap : allCombinationList)
{
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
for (Map.Entry<String, Object> entry : currentInputMap.entrySet())
{
//请求参数
NameValuePair nameValuePair = new NameValuePair();
nameValuePair.setName(entry.getKey());
nameValuePair.setValue(entry.getValue().toString());
nameValuePairList.add(nameValuePair);
}
String fileOutputPath = getFilePath(mapper.getThePath(), currentInputMap);
Page page = pageDao.findByThePath(mapper, fileOutputPath);
if(page != null && page.getTheVersion() == mapper.getTheVersion())
{
continue;
}
if(page == null)
{
page = new Page(mapper, fileOutputPath);
}
else
{
page.setTheVersion(mapper.getTheVersion());
}
//发送请求获取静态化数据
NameValuePair[] nameValuePairs = nameValuePairList.toArray(new NameValuePair[nameValuePairList.size()]);
JsonObject dataJsonObj = jsonUtil.jsonStrToJsonObj(httpFormGetSubmit.submitForm(mapper.getDataUrlInterface(), nameValuePairs));
if(!S_NormalFlag.success.equals(dataJsonObj.get(S_NormalFlag.result).getAsString()))
{
return MyBackInfo.fail(properties, dataJsonObj.get(S_NormalFlag.info).getAsString());
}
//执行静态化
freemarkerStaticUtil.createFile(mapper.getTemplatePath(), fileOutputPath,
dataJsonObj, orgInputMap, currentInputMap);
pageDao.save(page);
}
}
catch (Exception e)
{
e.printStackTrace();
return MyBackInfo.fail(properties, "模板静态化失败");
}
properties.put(S_NormalFlag.result, S_NormalFlag.success);
properties.put(S_NormalFlag.info, S_NormalFlag.info_Success);
return properties;
}
/**
 * 通过文件路径模板和输入参数,生成需要静态化的文件路径
 * @param thePathTemplate 文件输出路径 "http://test.qin_{p0}_iu.com/NewsDetail/{p1}.html"
 * @param thePathTemplate 文件输出路径 "http://test.qin_{p0}_iu.com/NewsList/{p1}/{p2}/{p3}.html"
 * @param parameterMap 请求时输入参数键值对
 * @return 文件名称
 */
public String getFilePath(String thePathTemplate, Map<String, Object> parameterMap)
{
String regex = "\\{([\\w]+)\\}";
Pattern pattern =  Pattern.compile(regex);
Matcher matcher = pattern.matcher(thePathTemplate);
while(matcher.find())
{
String rStr = matcher.group().replaceAll(regex, "$1");
if(parameterMap.containsKey(rStr))
{
thePathTemplate = thePathTemplate.replaceAll("\\{"+rStr+"\\}", parameterMap.get(rStr).toString());
}
}
return thePathTemplate;
}
}

//模板
public class Mapper implements Serializable
{
private Long tableId;
private Long version;   //乐观锁版本号
private String templatePath; //模板在业务服务器端的相对路径
private String thePath; //设置根据模板生成的文件在云存储端的路径规则,例如:NewsDetail/{id}_{theVersion}.html
private String jsonParamStr; //json格式的通配输入参数格式,例如:{id:newsIdList}
private String dataUrlInterface;//请求数据接口
private Long theVersion; //版本号
private Integer theState; //状态 S_State
private Long createDatetimeStamp;
}

猜你喜欢

转载自blog.csdn.net/namelessfighter/article/details/80547265
今日推荐