Session aSession = null;
Transaction tx = null;
try {
aSession = getSession();
tx = aSession.beginTransaction();
SbiDataSetHistory hibDataSet =null;
if(dataSet!=null){
Integer dsId = dataSet.getDsId();
GuiDataSetDetail dsActiveDetailToSet = dataSet.getActiveDetail();
if(dsActiveDetailToSet instanceof FileDataSetDetail){
hibDataSet=new SbiFileDataSet();
if(((FileDataSetDetail)dsActiveDetailToSet).getFileName()!=null){
((SbiFileDataSet)hibDataSet).setFileName(((FileDataSetDetail)dsActiveDetailToSet).getFileName());
}
}
else if(dsActiveDetailToSet instanceof QueryDataSetDetail){
hibDataSet=new SbiQueryDataSet();
if(((QueryDataSetDetail)dsActiveDetailToSet).getQuery()!=null){
((SbiQueryDataSet)hibDataSet).setQuery(((QueryDataSetDetail)dsActiveDetailToSet).getQuery().toString());
}
if(((QueryDataSetDetail)dsActiveDetailToSet).getDataSourceLabel()!=null){
SbiDataSource hibDataSource = null;
String dataSourceLabel = ((QueryDataSetDetail)dsActiveDetailToSet).getDataSourceLabel();
Criterion labelCriterrion = Expression.eq("label", dataSourceLabel);
Criteria criteria = aSession.createCriteria(SbiDataSource.class);
criteria.add(labelCriterrion);
hibDataSource = (SbiDataSource) criteria.uniqueResult();
((SbiQueryDataSet)hibDataSet).setDataSource(hibDataSource);
}
}
else if (dsActiveDetailToSet instanceof QbeDataSetDetail) {
hibDataSet = new SbiQbeDataSet();
SbiQbeDataSet hibQbeDataSet = (SbiQbeDataSet) hibDataSet;
QbeDataSetDetail qbeDataSet = (QbeDataSetDetail) dsActiveDetailToSet;
hibQbeDataSet.setSqlQuery(qbeDataSet.getSqlQuery());
hibQbeDataSet.setJsonQuery(qbeDataSet.getJsonQuery());
hibQbeDataSet.setDatamarts(qbeDataSet.getDatamarts());
String dataSourceLabel = qbeDataSet.getDataSourceLabel();
Criterion labelCriterrion = Expression.eq("label", dataSourceLabel);
Criteria criteria = aSession.createCriteria(SbiDataSource.class);
criteria.add(labelCriterrion);
SbiDataSource hibDataSource = (SbiDataSource) criteria.uniqueResult();
hibQbeDataSet.setDataSource(hibDataSource);
}
else if(dsActiveDetailToSet instanceof WSDataSetDetail){
hibDataSet=new SbiWSDataSet();
if(((WSDataSetDetail)dsActiveDetailToSet).getAddress()!=null){
((SbiWSDataSet)hibDataSet).setAdress(((WSDataSetDetail)dsActiveDetailToSet).getAddress());
}
if(((WSDataSetDetail)dsActiveDetailToSet).getOperation()!=null){
((SbiWSDataSet)hibDataSet).setOperation(((WSDataSetDetail)dsActiveDetailToSet).getOperation());
}
}
else if(dsActiveDetailToSet instanceof JClassDataSetDetail){
hibDataSet=new SbiJClassDataSet();
if(((JClassDataSetDetail)dsActiveDetailToSet).getJavaClassName()!=null){
((SbiJClassDataSet)hibDataSet).setJavaClassName(((JClassDataSetDetail)dsActiveDetailToSet).getJavaClassName());
}
}
else if(dsActiveDetailToSet instanceof ScriptDataSetDetail){
hibDataSet=new SbiScriptDataSet();
if(((ScriptDataSetDetail)dsActiveDetailToSet).getScript()!=null){
((SbiScriptDataSet)hibDataSet).setScript(((ScriptDataSetDetail)dsActiveDetailToSet).getScript());
}
if(((ScriptDataSetDetail)dsActiveDetailToSet).getLanguageScript()!=null){
((SbiScriptDataSet)hibDataSet).setLanguageScript(((ScriptDataSetDetail)dsActiveDetailToSet).getLanguageScript());
}
}
SbiDomains transformer = null;
if (dsActiveDetailToSet.getTransformerId() != null){
Criterion aCriterion = Expression.eq("valueId", dsActiveDetailToSet.getTransformerId());
Criteria criteria = aSession.createCriteria(SbiDomains.class);
criteria.add(aCriterion);
transformer = (SbiDomains) criteria.uniqueResult();
if (transformer == null){
logger.error("The Domain with value_id= "+dsActiveDetailToSet.getTransformerId()+" does not exist.");
throw new EMFUserError(EMFErrorSeverity.ERROR, 1035);
}
}
SbiDomains category = null;
if (dsActiveDetailToSet.getCategoryId()!= null){
Criterion aCriterion = Expression.eq("valueId", dsActiveDetailToSet.getCategoryId());
Criteria criteria = aSession.createCriteria(SbiDomains.class);
criteria.add(aCriterion);
category = (SbiDomains) criteria.uniqueResult();
if (category == null){
logger.error("The Domain with value_id= "+dsActiveDetailToSet.getCategoryId()+" does not exist.");
throw new EMFUserError(EMFErrorSeverity.ERROR, 1035);
}
}
Date currentTStamp = new Date();
hibDataSet.setTimeIn(currentTStamp);
hibDataSet.setActive(true);
hibDataSet.setTransformer(transformer);
hibDataSet.setPivotColumnName(dsActiveDetailToSet.getPivotColumnName());
hibDataSet.setPivotRowName(dsActiveDetailToSet.getPivotRowName());
hibDataSet.setPivotColumnValue(dsActiveDetailToSet.getPivotColumnValue());
hibDataSet.setNumRows(dsActiveDetailToSet.isNumRows());
hibDataSet.setCategory(category);
hibDataSet.setParameters(dsActiveDetailToSet.getParameters());
hibDataSet.setDsMetadata(dsActiveDetailToSet.getDsMetadata());
SbiDataSetConfig hibGenericDataSet = (SbiDataSetConfig) aSession.load(SbiDataSetConfig.class,dsId);
hibGenericDataSet.setLabel(dataSet.getLabel());
hibGenericDataSet.setDescription(dataSet.getDescription());
hibGenericDataSet.setName(dataSet.getName());
updateSbiCommonInfo4Update(hibGenericDataSet);
String userUp = hibGenericDataSet.getCommonInfo().getUserUp();
String sbiVersionUp = hibGenericDataSet.getCommonInfo().getSbiVersionUp();
hibDataSet.setUserIn(userUp);
hibDataSet.setSbiVersionIn(sbiVersionUp);
hibDataSet.setTimeIn(currentTStamp);
Integer currenthigherVersion = getHigherVersionNumForDS(dsId);
Integer newVersion = currenthigherVersion+1;
hibDataSet.setVersionNum(newVersion);
Query hibQuery = aSession.createQuery("from SbiDataSetHistory h where h.active = ? and h.sbiDsConfig = ?" );
hibQuery.setBoolean(0, true);
hibQuery.setInteger(1, dsId);
SbiDataSetHistory dsActiveDetail =(SbiDataSetHistory)hibQuery.uniqueResult();
dsActiveDetail.setActive(false);
aSession.update(dsActiveDetail);
aSession.update(hibGenericDataSet);
hibDataSet.setSbiDsConfig(hibGenericDataSet);
aSession.save(hibDataSet);