/* * This view displays a list of all ItemTypes available. * If the parameter itemTypeName is set a new item type will be created. */ public ModelAndView assoctypes( HttpServletRequest request, HttpServletResponse httpServletResponse) { ModelAndView mav = new ModelAndView("easyrec/assoctypes"); Operator signedInOperator = Security.signedInOperator(request); RemoteTenant remoteTenant = viewInitializationService.initializeView(request, mav); String assocTypeName = ServletUtils.getSafeParameter(request, "assocTypeName", "").toUpperCase(); if (signedInOperator != null) { if (!"".equals(assocTypeName)) { // create a new item type if the itemTypeName parameter is set String error = isValidAssocTypeName(assocTypeName); if ("".equals(error)) { tenantService.insertAssocTypeForTenant(remoteTenant.getId(), assocTypeName, true); } else { mav.addObject("error", error); } } mav.addObject("apiKey", signedInOperator.getApiKey()); mav.addObject("assocTypes", assocTypeDAO.getTypes(remoteTenant.getId(), true)); mav.addObject( "tenants", remoteTenantDAO.getTenantsFromOperator(remoteTenant.getOperatorId())); } return mav; }
@Override protected ModelAndView onSubmit( HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { ModelAndView mav = new ModelAndView("page"); RemoteTenant remoteTenant = viewInitializationService.initializeView(request, mav); FileUploadBean bean = (FileUploadBean) command; // cast the bean if (bean == null || bean.getFile() == null || bean.getFile().isEmpty()) { logger.info("no file or empty file was uploaded, aborting"); // well, let's do nothing with the bean for now and return return super.onSubmit(request, response, command, errors); } // check if there's content there MultipartFile file = bean.getFile(); CsvInput csvInput = new CsvInput(";", null); csvInput.setSource(file.getInputStream(), "UTF-8"); List<String> validationErrors = new ArrayList<String>(); List<String> touchedClusters = new ArrayList<String>(); int lineNumber = 0; int numberOfItems = 0; int numberOfTouchedClusters = 0; int tenantId = 0; while (csvInput.hasNext()) { lineNumber++; List<String> fields = null; try { fields = csvInput.next(); // read one line } catch (InconsistentFieldCountException e) { validationErrors.add( "ERROR in line number " + lineNumber + ": column numbers don't equal headerline columns!"); continue; } if (lineNumber == 1) { continue; } // header line boolean lineError = false; if (fields.size() < 3) { validationErrors.add( "ERROR in line number " + lineNumber + ": column numbers smaller then 3!"); continue; } // init the CSV Columns to Java Variables String clusterName = fields.get(0); String itemId = fields.get(1); String itemType = fields.get(2); // the following code is used to validate the CSV line ... if (clusterName.equals("")) { validationErrors.add("ERROR in line number " + lineNumber + ": clusterName is missing."); lineError = true; } if (itemId.equals("")) { validationErrors.add("ERROR in line number " + lineNumber + ": itemId is missing."); lineError = true; } if (itemType.equals("")) { validationErrors.add("ERROR in line number " + lineNumber + ": itemType is missing."); lineError = true; } ClusterVO cluster = clusterService.loadCluster(remoteTenant.getId(), clusterName); if (cluster == null) { validationErrors.add( "ERROR in line number " + lineNumber + ": cluster with name'" + clusterName + "' does not exist. Please create before importing!"); lineError = true; } Integer itemTypeId = itemTypeDAO.getIdOfType(remoteTenant.getId(), itemType); Item item = itemDAO.get(remoteTenant, itemId, itemType); if (null == item) { validationErrors.add( "ERROR in line number " + lineNumber + ": item with id '" + itemId + "' and type '" + itemType + "' does not exist."); lineError = true; } if (lineError) { continue; } Integer itemIdInt = idMappingDAO.lookup(item.getItemId()); try { clusterService.addItemToCluster(remoteTenant.getId(), clusterName, itemIdInt, itemTypeId); } catch (ClusterException ex) { validationErrors.add("ERROR in line number " + lineNumber + ": " + ex.getMessage()); continue; } numberOfItems++; if (!touchedClusters.contains(clusterName)) { numberOfTouchedClusters++; touchedClusters.add(clusterName); } } mav.addObject("tenant", remoteTenant); mav.addObject("page", "clustermanager/import/importResult"); mav.addObject("numberOfItems", numberOfItems); mav.addObject("numberOfTouchedClusters", numberOfTouchedClusters); mav.addObject("validationErrors", validationErrors); return mav; }