	public ModelAndView saveflow(final HttpServletRequest req, HttpServletResponse res) throws MeandreServerException{
		String token=System.currentTimeMillis()+"-token";
		HashMap<String,String> paramMap = new HashMap<String,String>();
		boolean isMultipart = ServletFileUpload.isMultipartContent(req);
		if(!isMultipart){
			LOGGER.severe("Error -this should be multipart");
		}
		DiskFileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);
		String uploadDir = getServletContext().getRealPath(getUploadDirectory()) + "/" + req.getRemoteUser() + "/"+ token+"/";
	     File dirPath = new File(uploadDir);
	      if (!dirPath.exists()) {
	         dirPath.mkdirs();
	      }
	      String flowId =  null;
		  String flowUri= null;
		try {
			List<FileItem> items = upload.parseRequest(req);
			Iterator<FileItem> iter = items.iterator();
			while (iter.hasNext()) {
			    FileItem item = iter.next();
			    if (item.isFormField()) {
			    	String name = item.getFieldName();
			        String value = item.getString();
			        paramMap.put(name, value);
			        if("flowTemplateId".equals(name)){
			        	flowId = value;
			        }else if("flowTemplateUri".equals(name)){
			        	 flowUri = value;
			        }
			    } else {
			    	String fieldName = item.getFieldName();
			        String fileName = item.getName();
			        String contentType = item.getContentType();
			        boolean isInMemory = item.isInMemory();
			        long sizeInBytes = item.getSize();
			        if(fileName!=null && sizeInBytes>0 && fileName.length()>0){
			        	File uploadedFile = new File(uploadDir+File.separator + fileName);
			        	item.write(uploadedFile);
			        	System.out.println("file uploaded: "+ fileName + uploadedFile.getAbsolutePath());
			        	String webDir = uploadDir.substring(getServletContext().getRealPath("/").length());
			        	paramMap.put(fieldName,"http://"+ req.getServerName()+":"+req.getServerPort()+ req.getContextPath()+webDir+fileName);
			        }
			    }
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(flowId==null || flowUri==null){
			LOGGER.severe("flowId or flowUri is null -some severe error happened...");
		}
		String newFlowUri=flowService.createNewFlow(paramMap,flowUri);
		Long longFlowId  =Long.parseLong(flowId);
		Flow templateFlow = this.getFlowService().getFlow(longFlowId );
		String name = paramMap.get("name");
		String description = paramMap.get("description");
		if(name ==null){
			name =templateFlow.getName()+File.separator+token;
		}
		if(description==null){
			description = templateFlow.getDescription()+" for flow: "+token;
		}
		User user = userManager.getCurrentUser();
		logger.debug("USER IS ====> " + user);
		if (user == null) {
			user = userManager.getUserByUsername("admin");
		}
		long userId = user.getId();
		Flow instance = new Flow();
		instance.setCreatorId(userId);
		instance.setDateCreated(new Date());
		instance.setInstanceOf(templateFlow);
		instance.setKeyWords(templateFlow.getKeyWords());
		instance.setName(name);
		instance.setTemplate(false);
		instance.setUrl(newFlowUri);
		instance.setDescription(description);
		instance.setType(FlowTypeUtils.DEFAULT_FLOW);
		System.out.println("The new flow uri is: " + newFlowUri);
		long instanceId=this.getFlowService().storeFlowInstance(instance);
		Job job=this.getFlowService().executeJob(token, name,description, instanceId, user.getId(), user.getEmail());
		ModelAndView mav= new ModelAndView(new RedirectView("JobManager.jobDetail",true));
		mav.addObject("id", job.getId());
		return mav;
	}

	public ModelAndView saveflow(final HttpServletRequest req, HttpServletResponse res) throws MeandreServerException{
		String token=System.currentTimeMillis()+"-token";
		HashMap<String,String> paramMap = new HashMap<String,String>();
		boolean isMultipart = ServletFileUpload.isMultipartContent(req);
		if(!isMultipart){
			LOGGER.severe("Error -this should be multipart");
		}
		DiskFileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);
		String uploadDir = getServletContext().getRealPath(getUploadDirectory()) + "/" + req.getRemoteUser() + "/"+ token+"/";
	     File dirPath = new File(uploadDir);
	      if (!dirPath.exists()) {
	         dirPath.mkdirs();
	      }
	      String flowId =  null;
		  String flowUri= null;
		try {
			List<FileItem> items = upload.parseRequest(req);
			Iterator<FileItem> iter = items.iterator();
			while (iter.hasNext()) {
			    FileItem item = iter.next();
			    if (item.isFormField()) {
			    	String name = item.getFieldName();
			        String value = item.getString();
			        paramMap.put(name, value);
			        if("flowTemplateId".equals(name)){
			        	flowId = value;
			        }else if("flowTemplateUri".equals(name)){
			        	 flowUri = value;
			        }
			    } else {
			    	String fieldName = item.getFieldName();
			        String fileName = item.getName();
			        String contentType = item.getContentType();
			        boolean isInMemory = item.isInMemory();
			        long sizeInBytes = item.getSize();
			        if(fileName!=null && sizeInBytes>0 && fileName.length()>0){
			        	File uploadedFile = new File(uploadDir+File.separator + fileName);
			        	item.write(uploadedFile);
			        	System.out.println("file uploaded: "+ fileName + uploadedFile.getAbsolutePath());
			        	String webDir = uploadDir.substring(getServletContext().getRealPath("/").length());
			        	paramMap.put(fieldName,"http://"+ req.getServerName()+":"+req.getServerPort()+ req.getContextPath()+webDir+fileName);
			        }
			    }
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(flowId==null || flowUri==null){
			LOGGER.severe("flowId or flowUri is null -some severe error happened...");
		}
		String newFlowUri=flowService.createNewFlow(paramMap,flowUri);
		Long longFlowId  =Long.parseLong(flowId);
		Flow templateFlow = this.getFlowService().getFlow(longFlowId );
		String name = paramMap.get("name");
		String description = paramMap.get("description");
		if(name ==null){
			name =templateFlow.getName()+File.separator+token;
		}
		if(description==null){
			description = templateFlow.getDescription()+" for flow: "+token;
		}
		User user = userManager.getCurrentUser();
		logger.debug("USER IS ====> " + user);
		if (user == null) {
			user = userManager.getUserByUsername("admin");
		}
		long userId = user.getId();
		Flow instance = new Flow();
		instance.setCreatorId(userId);
		instance.setDateCreated(new Date());
		instance.setInstanceOf(templateFlow);
		instance.setKeyWords(templateFlow.getKeyWords());
		instance.setName(name);
		instance.setTemplate(false);
		instance.setUrl(newFlowUri);
		instance.setDescription(description);
		instance.setType(Flow.FlowType.INHERITS);
		instance.setTypeName(Flow.FlowType.INHERITS.getName());
		System.out.println("The new flow uri is: " + newFlowUri);
		long instanceId=this.getFlowService().storeFlowInstance(instance);
		Job job=this.getFlowService().executeJob(token, name,description, instanceId, user.getId(), user.getEmail());
		ModelAndView mav= new ModelAndView(new RedirectView("JobManager.jobDetail",true));
		mav.addObject("id", job.getId());
		return mav;
	}
