String viewpointScope;
String viewpointOwner;
JSONObject viewpointJSON;
String viewpointString;
IViewpointDAO viewpointDAO;
Viewpoint viewpoint;
logger.debug("IN");
try{
viewpointName = getAttributeAsString(NAME);
viewpointDescription = getAttributeAsString(DESCRIPTION);
viewpointScope = getAttributeAsString(SCOPE);
viewpointJSON = getAttributeAsJSONObject( VIEWPOINT );
logger.debug("Parameter [" + NAME + "] is equals to [" + viewpointName + "]");
logger.debug("Parameter [" + DESCRIPTION + "] is equals to [" + viewpointDescription + "]");
logger.debug("Parameter [" + SCOPE + "] is equals to [" + viewpointScope + "]");
logger.debug("Parameter [" + viewpointScope + "] is equals to [" + viewpointJSON + "]");
Assert.assertTrue(!StringUtilities.isEmpty(viewpointScope), "Viewpoint's name cannot be null or empty");
Assert.assertNotNull(!StringUtilities.isEmpty(viewpointDescription), "Viewpoint's description cannot be null or empty");
Assert.assertNotNull(!StringUtilities.isEmpty(viewpointScope), "Viewpoint's scope cannot be null or empty");
Assert.assertNotNull(viewpointJSON, "Viewpoint's content cannot be null");
executionInstance = getContext().getExecutionInstance( ExecutionInstance.class.getName() );
Assert.assertNotNull(executionInstance, "Execution instance cannot be null");
userProfile = this.getUserProfile();
Assert.assertNotNull(userProfile, "Impossible to retrive user profile");
biobjectId = executionInstance.getBIObject().getId();
Assert.assertNotNull(executionInstance, "Impossible to retrive analytical document id");
logger.debug("User: [" + userProfile.getUserUniqueIdentifier() + "]");
logger.debug("Document Id: [" + biobjectId + "]");
viewpointOwner = (String) ((UserProfile)userProfile).getUserId();
Iterator it = viewpointJSON.keys();
Assert.assertTrue(it.hasNext(), "Viewpoint's content cannot be empty");
viewpointString = "";
while (it.hasNext()) {
String parameterName = (String) it.next();
String parameterValue;
try {
parameterValue = viewpointJSON.getString( parameterName );
} catch (JSONException e) {
logger.error("Impossible read value for the parameter [" + parameterName + "] into viewpoint's content", e);
throw new SpagoBIServiceException(SERVICE_NAME, "Impossible read value for the parameter [" + parameterName + "] into viewpoint's content", e);
}
// defines the string of parameters to save into db
if(!StringUtilities.isEmpty(parameterValue)) {
viewpointString += parameterName + "%3D" + parameterValue + "%26";
}
}
if (viewpointString.endsWith("%26")) {
viewpointString = viewpointString.substring(0, viewpointString.length() - 3);
}
logger.debug("Viewpoint's content will be saved on database as: [" + viewpointString + "]");
try {
viewpointDAO = DAOFactory.getViewpointDAO();
viewpoint = viewpointDAO.loadViewpointByNameAndBIObjectId(viewpointName, biobjectId);
if (viewpoint != null) throw new SpagoBIServiceException(SERVICE_NAME, "A viewpoint with the name [" + viewpointName + "] alredy exist");
//Assert.assertTrue(viewpoint == null, "A viewpoint with the name [" + viewpointName + "] alredy exist");
} catch (EMFUserError e) {
logger.error("Impossible to check if a viewpoint with name [" + viewpointName + "] already exists", e);
throw new SpagoBIServiceException(SERVICE_NAME, "Impossible to check if a viewpoint with name [" + viewpointName + "] already exists", e);
}
try {
viewpointDAO = DAOFactory.getViewpointDAO();
viewpointDAO.setUserProfile(userProfile);
viewpoint = new Viewpoint();
viewpoint.setBiobjId( biobjectId );
viewpoint.setVpName( viewpointName );
viewpoint.setVpOwner( viewpointOwner );
viewpoint.setVpDesc( viewpointDescription );
viewpoint.setVpScope( viewpointScope );
viewpoint.setVpValueParams( viewpointString );
viewpoint.setVpCreationDate(new Timestamp(System.currentTimeMillis()));
viewpointDAO.insertViewpoint(viewpoint);
} catch (EMFUserError e) {
logger.error("Impossible to save viewpoint [" + viewpointName + "]", e);
throw new SpagoBIServiceException(SERVICE_NAME, "Impossible to check if a viewpoint with name [" + viewpointName + "] already exists", e);
}