public String mountScriptTemplate(HttpServletRequest request,HttpServletResponse response) throws Exception {
String scriptId = request.getParameter("scriptId");
String templateId = request.getParameter("templateId");
String actions = request.getParameter("actions");
String stats = request.getParameter("stats");
Script script = das.queryScript(DataAccessFactory.getInstance().createUUID(scriptId));
UUID[] oldTemplateIds = script.getTemplateIds();
if(!CynthiaUtil.idInArray(oldTemplateIds,templateId))
{
List<UUID> oldTemplateIdsList = new ArrayList<UUID>();
if(oldTemplateIds!=null)
{
for(UUID oldTemplateId : oldTemplateIds)
{
oldTemplateIdsList.add(oldTemplateId);
}
}
if(templateId!=null&&!templateId.equals("")&&!"null".equals(templateId))
oldTemplateIdsList.add(DataAccessFactory.getInstance().createUUID(templateId));
script.setTemplateIds(oldTemplateIdsList.toArray(new UUID[oldTemplateIdsList.size()]));
}
Template newTemplate = das.queryTemplate(DataAccessFactory.getInstance().createUUID(templateId));
Flow newFlow = das.queryFlow(newTemplate.getFlowId());
Stat[] newFlowStats= newFlow.getStats();
Action[] newFlowActions=newFlow.getActions();
List<UUID> newFlowStatIds = new ArrayList<UUID>();
List<UUID> newFlowActionIds = new ArrayList<UUID>();
for(Stat stat : newFlowStats)
{
newFlowStatIds.add(stat.getId());
}
for(Action action : newFlowActions)
{
newFlowActionIds.add(action.getId());
}
if(actions!=null)
{
UUID[] oldActionIds = script.getActionIds();
List<UUID> oldActionIdsList = new ArrayList<UUID>();
if(oldActionIds != null)
{
for(UUID oldActionId : oldActionIds)
{
oldActionIdsList.add(oldActionId);
}
}
UUID[] newActionIds = CynthiaUtil.stringToIdArray(actions);
for(UUID actionId : newActionIds)
{
if(!oldActionIdsList.contains(actionId))
{
oldActionIdsList.add(actionId);
}
newFlowActionIds.remove(actionId);
}
for(UUID actionId : newFlowActionIds)
{
if(oldActionIdsList.contains(actionId))
oldActionIdsList.remove(actionId);
}
script.setActionIds(oldActionIdsList.toArray(new UUID[oldActionIdsList.size()]));
}
if(stats != null)
{
UUID[] oldStatIds = script.getEndStatIds();
List<UUID> oldStatIdsList = new ArrayList<UUID>();
if(oldStatIds != null)
{
for(UUID oldStatId : oldStatIds)
{
oldStatIdsList.add(oldStatId);
}
}
UUID[] newStatIds = CynthiaUtil.stringToIdArray(stats);
for(UUID statId : newStatIds)
{
if(!oldStatIdsList.contains(statId))
{
oldStatIdsList.add(statId);
}
newFlowStatIds.remove(statId);
}
for(UUID statId : newFlowStatIds)
{
if(oldStatIdsList.contains(statId))
oldStatIdsList.remove(statId);
}
script.setEndStatIds(oldStatIdsList.toArray(new UUID[oldStatIdsList.size()]));
}
das.updateScript(script);
StringBuffer result = new StringBuffer(this.baseXml);
result.append("<root>");
result.append("<id>").append(script.getId()).append("</id>");
result.append("</root>");
return result.toString();
}