BIObject o = new BIObject();
BIObject objAlreadyExisting = objDao.loadBIObjectByLabel(label);
if(objAlreadyExisting!=null){
logger.error("Document with the same label already exists");
throw new SpagoBIServiceException(SERVICE_NAME, "sbi.document.labelAlreadyExistent");
}
o.setName(name);
o.setLabel(label);
o.setDescription(description);
o.setVisible(new Integer(1));
if(engineId!=null){
Engine engine = DAOFactory.getEngineDAO().loadEngineByID(new Integer(engineId));
o.setEngine(engine);
}else{
List<Engine> engines = DAOFactory.getEngineDAO().loadAllEnginesForBIObjectType(type);
if(engines!=null && !engines.isEmpty()){
o.setEngine(engines.get(0));
}
}
Domain objType = DAOFactory.getDomainDAO().loadDomainByCodeAndValue(SpagoBIConstants.BIOBJ_TYPE, type);
Integer biObjectTypeID = objType.getValueId();
o.setBiObjectTypeID(biObjectTypeID);
o.setBiObjectTypeCode(objType.getValueCd());
UserProfile userProfile = (UserProfile) this.getUserProfile();
String creationUser = userProfile.getUserId().toString();
o.setCreationUser(creationUser);
if(dataSourceId!=null && dataSourceId!=""){
o.setDataSourceId(new Integer(dataSourceId));
}
List<Integer> functionalities = new ArrayList<Integer>();
for(int i=0; i< functsArrayJSon.length(); i++){
String funcIdStr = functsArrayJSon.getString(i);
Integer funcId = new Integer(funcIdStr);
if (funcId.intValue() == -1) {
// -1 stands for personal folder: check is it exists
boolean exists = UserUtilities.userFunctionalityRootExists(userProfile);
if (!exists) {
// create personal folder if it doesn't exist
UserUtilities.createUserFunctionalityRoot(userProfile);
}
// load personal folder to get its id
LowFunctionality lf = UserUtilities.loadUserFunctionalityRoot(userProfile);
funcId = lf.getId();
}
functionalities.add(funcId);
}
o.setFunctionalities(functionalities);
Domain objState = DAOFactory.getDomainDAO().loadDomainByCodeAndValue(SpagoBIConstants.DOC_STATE, SpagoBIConstants.DOC_STATE_REL);
Integer stateID = objState.getValueId();
o.setStateID(stateID);
o.setStateCode(objState.getValueCd());
BIObject orig_obj = objDao.loadBIObjectById(new Integer(orig_biobj_id));
ObjTemplate objTemp = new ObjTemplate();
byte[] content = null;
if(template != null && template != ""){
content = template.getBytes();
}else if(smartFilterValues!=null){
content = getSmartFilterTemplateContent();
}else if(wk_definition!=null && query!=null && orig_obj!=null){
ObjTemplate qbETemplate = orig_obj.getActiveTemplate();
String templCont = new String(qbETemplate.getContent());
WorksheetDriver q = new WorksheetDriver();
String temp = q.composeWorksheetTemplate(wk_definition, query, null, templCont);
content = temp.getBytes();
}else{
logger.error("Document template not available");
throw new SpagoBIServiceException(SERVICE_NAME, "sbi.document.saveError");
}
objTemp.setContent(content);
objTemp.setCreationUser(creationUser);
objTemp.setDimension(Long.toString(content.length/1000)+" KByte");
objTemp.setName("template.sbiworksheet");
try {
if(id != null && !id.equals("") && !id.equals("0")){
o.setId(Integer.valueOf(id));
objDao.modifyBIObject(o, objTemp);
logger.debug("Document with id "+id+" updated");
JSONObject attributesResponseSuccessJSON = new JSONObject();
attributesResponseSuccessJSON.put("success", true);
attributesResponseSuccessJSON.put("responseText", "Operation succeded");
writeBackToClient( new JSONSuccess(attributesResponseSuccessJSON) );
}else{
Integer biObjectID = objDao.insertBIObject(o, objTemp);
if(orig_biobj_id!=null && orig_biobj_id!=""){
List obj_pars = orig_obj.getBiObjectParameters();
if(obj_pars!=null && !obj_pars.isEmpty()){
Iterator it = obj_pars.iterator();
while(it.hasNext()){
BIObjectParameter par = (BIObjectParameter)it.next();
par.setBiObjectID(biObjectID);
par.setId(null);
DAOFactory.getBIObjectParameterDAO().insertBIObjectParameter(par);
}
}
}
logger.debug("New document inserted");
JSONObject attributesResponseSuccessJSON = new JSONObject();
attributesResponseSuccessJSON.put("responseText", "Operation succeded");
writeBackToClient( new JSONSuccess(attributesResponseSuccessJSON) );
}
} catch (Throwable e) {
logger.error(e.getMessage(), e);
throw new SpagoBIServiceException(SERVICE_NAME,"sbi.document.saveError", e);
}
} else {
logger.error("Document name or label are missing");
throw new SpagoBIServiceException(SERVICE_NAME, "sbi.document.missingFieldsError");
}
}