List of usage examples for org.apache.commons.lang3 StringUtils isNumeric
public static boolean isNumeric(final CharSequence cs)
Checks if the CharSequence contains only Unicode digits.
From source file:org.pad.pgsql.loadmovies.LoadFiles.java
/** * Load movies from csv file and save them in DB. * * @throws Exception/* w w w .j a v a 2 s .c o m*/ */ private static void loadMoviesAndLinks() throws Exception { MovieDao movieDao = new MovieDao(DS); Map<Integer, Integer[]> moviesLinks = new HashMap<>(); //Loads all links informations in memory to enrich afterwards movies CSVParser parser = new CSVParser(new FileReader("C:\\PRIVE\\SRC\\ml-20m\\links.csv"), CSVFormat.EXCEL.withHeader()); for (CSVRecord link : parser) { Integer movieId = Integer.parseInt(link.get("movieId")); if (keepId(movieId)) { System.out.println("Parsing line " + link.toString()); Integer[] otherIds = new Integer[2]; otherIds[0] = Integer.parseInt(link.get("imdbId")); if (StringUtils.isNoneEmpty(link.get("tmdbId"))) { otherIds[1] = Integer.parseInt(link.get("tmdbId")); } moviesLinks.put(movieId, otherIds); } } //Read movie file final Reader reader = new FileReader("C:\\PRIVE\\SRC\\ml-20m\\movies.csv"); parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader()); for (CSVRecord record : parser) { //build a movie object from record Integer movieId = Integer.parseInt(record.get("movieId")); if (keepId(movieId)) { String title = record.get("title"); String genres = record.get("genres"); //Splitting title to extract the date String movieDate = StringUtils.substringBeforeLast(StringUtils.substringAfterLast(title, "("), ")"); String movieName = null; if (StringUtils.isNumeric(movieDate)) { movieName = StringUtils.substringBeforeLast(title, "("); } else { movieName = title; movieDate = null; } System.out.println(movieName + " - " + movieDate); Movie movieToAdd = new Movie(movieId, movieName, movieDate); //Enrich movie with links Integer[] additionalIds = moviesLinks.get(movieId); if (additionalIds != null) { movieToAdd.setImdbId(additionalIds[0]); movieToAdd.setTmdbId(additionalIds[1]); } //Save in database movieDao.save(movieToAdd); } } }
From source file:org.paxml.selenium.rc.AccountNumberValidator.java
public static boolean validate(String value) { value = value.replaceAll("[.\\s]", ""); boolean isNumeric = StringUtils.isNumeric(value); int len = value.length(); if (len >= MIN_ACCOUNTNUMBER_LENGTH && len <= MAX_ACCOUNTNUMBER_LENGTH) { // regular account if (!isNumeric || !modula11(value)) { return false; } else if (StringUtils.containsOnly(value, "0")) { // check if only zeros return false; }//from www . j av a2s .c om } else if (len >= MIN_POSTACCOUNTNUMBER_LENGTH && len <= MAX_POSTACCOUNTNUMBER_LENGTH) { // post bank if (StringUtils.containsOnly(value, "pP0")) { // check if one of these: p, P, p0, P0, 0, 0000000 return false; } else if (len == MAX_POSTACCOUNTNUMBER_LENGTH && isNumeric) { // exactly 8 digits is an invalid account number return false; } else if (!isNumeric) { // contains letters, check if it is correct one in correct position int noOfLetters = value.replaceAll("\\d", "").length(); if (noOfLetters > 1) { // more then one letter is a problem return false; } else if (!value.substring(0, 1).equalsIgnoreCase("P")) { // P must be first if present return false; } } } else { // value too long or empty (it can happen, e.g.: ". . .") return false; } return true; }
From source file:org.paxml.tag.ConstTagFactory.java
/** * {@inheritDoc}/* www . j ava 2 s.c om*/ */ @Override protected boolean populate(final T tag, IParserContext context) { final OMElement ele = context.getElement(); final String tagName = ele.getLocalName(); if (tagName.contains(".")) { throw new PaxmlRuntimeException("Data tag name should not contain dots"); } if (StringUtils.isNumeric(tagName.substring(0, 1))) { throw new PaxmlRuntimeException("Data tag name should not start with number"); } super.populate(tag, context); tag.setValueName(tagName); tag.setSubconst(isUnderConst(tag)); if (isLocalConst(tag)) { if (tag.getIdExpression() == null) { throw new PaxmlRuntimeException("Local constant should have id attribute: <" + tagName + ">"); } tag.setScope(Scope.LOCAL); } else { // must be a parameter const here if (!tag.isSubconst()) { tag.setScope(Scope.PARAMETER); } } InvokerTagFactory.processElement(tag, context, new IAttributeFilter() { public boolean accept(OMElement ele, String attrName, String attrValue) { final boolean yes = tag.isSubconst() || !IdAttribute.DEFAULT_VALUE.equals(attrName); if (yes) { tag.addAttributeName(attrName); } return yes; } }); return false; }
From source file:org.paxml.tag.sql.DdlVersion.java
public DdlVersion(String version) { String[] vs = StringUtils.split(version, '.'); List<Integer> v = new ArrayList<Integer>(vs.length); for (int i = 0; i < vs.length; i++) { String s = vs[i];/* w ww. j a v a 2 s .c om*/ if (StringUtils.isNumeric(s)) { v.add(Integer.parseInt(s)); } else { break; } } this.version = v.toArray(new Integer[v.size()]); }
From source file:org.phenotips.data.internal.PatientBirthdateUpdater.java
/** * Read a property from the request./*w w w .ja v a2 s.co m*/ * * @param propertyName the name of the property as it would appear in the class, for example * {@code age_of_onset_years} * @param objectNumber the object's number * @return the value sent in the request, or {@code 0} if the property is missing */ private int getParameter(String propertyName, int objectNumber) { String parameterName = Constants.CODE_SPACE + ".PatientClass_" + objectNumber + "_" + propertyName; Request request = this.container.getRequest(); if (request == null) { return -1; } String value = (String) request.getProperty(parameterName); if ("-".equals(value)) { return -2; } if (!StringUtils.isNumeric(value)) { return -1; } return Integer.valueOf(value); }
From source file:org.phenotips.ontology.internal.GeneNomenclature.java
@Override public Set<OntologyTerm> search(Map<String, ?> fieldValues, Map<String, String> queryOptions) { try {/*w w w. j a v a 2 s. c om*/ HttpGet method = new HttpGet( searchServiceURL + URLEncoder.encode(generateQuery(fieldValues), Consts.UTF_8.name())); method.setHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.getMimeType()); try (CloseableHttpResponse httpResponse = this.client.execute(method)) { String response = IOUtils.toString(httpResponse.getEntity().getContent(), Consts.UTF_8); JSONObject responseJSON = (JSONObject) JSONSerializer.toJSON(response); JSONArray docs = responseJSON.getJSONObject(RESPONSE_KEY).getJSONArray(DATA_KEY); if (docs.size() >= 1) { Set<OntologyTerm> result = new LinkedHashSet<>(); // The remote service doesn't offer any query control, manually select the right range int start = 0; if (queryOptions.containsKey(CommonParams.START) && StringUtils.isNumeric(queryOptions.get(CommonParams.START))) { start = Math.max(0, Integer.parseInt(queryOptions.get(CommonParams.START))); } int end = docs.size(); if (queryOptions.containsKey(CommonParams.ROWS) && StringUtils.isNumeric(queryOptions.get(CommonParams.ROWS))) { end = Math.min(end, start + Integer.parseInt(queryOptions.get(CommonParams.ROWS))); } for (int i = start; i < end; ++i) { result.add(new JSONOntologyTerm(docs.getJSONObject(i), this)); } return result; // This is too slow, for the moment only return summaries // return getTerms(ids); } } catch (IOException ex) { this.logger.warn("Failed to search gene names: {}", ex.getMessage()); } } catch (UnsupportedEncodingException ex) { // This will not happen, UTF-8 is always available } return Collections.emptySet(); }
From source file:org.phenotips.vocabulary.internal.RemoteGeneNomenclature.java
@Override public List<VocabularyTerm> search(Map<String, ?> fieldValues, Map<String, String> queryOptions) { try {/*w w w .j av a2 s. com*/ HttpGet method = new HttpGet( this.searchServiceURL + URLEncoder.encode(generateQuery(fieldValues), Consts.UTF_8.name())); method.setHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.getMimeType()); try (CloseableHttpResponse httpResponse = this.client.execute(method)) { String response = IOUtils.toString(httpResponse.getEntity().getContent(), Consts.UTF_8); JSONObject responseJSON = new JSONObject(response); JSONArray docs = responseJSON.getJSONObject(RESPONSE_KEY).getJSONArray(DATA_KEY); if (docs.length() >= 1) { List<VocabularyTerm> result = new LinkedList<>(); // The remote service doesn't offer any query control, manually select the right range int start = 0; if (queryOptions.containsKey(CommonParams.START) && StringUtils.isNumeric(queryOptions.get(CommonParams.START))) { start = Math.max(0, Integer.parseInt(queryOptions.get(CommonParams.START))); } int end = docs.length(); if (queryOptions.containsKey(CommonParams.ROWS) && StringUtils.isNumeric(queryOptions.get(CommonParams.ROWS))) { end = Math.min(end, start + Integer.parseInt(queryOptions.get(CommonParams.ROWS))); } for (int i = start; i < end; ++i) { result.add(new JSONOntologyTerm(docs.getJSONObject(i), this)); } return result; // This is too slow, for the moment only return summaries // return getTerms(ids); } } catch (IOException | JSONException ex) { this.logger.warn("Failed to search gene names: {}", ex.getMessage()); } } catch (UnsupportedEncodingException ex) { // This will not happen, UTF-8 is always available } return Collections.emptyList(); }
From source file:org.polymap.wbv.mdb.GmkCsvPrinter.java
public GmkCsvPrinter() throws IOException { CsvPreference prefs = new CsvPreference('"', ',', "\r\n"); // quoteChar, delimiterChar, endOfLineSymbols // neue Gemarkungen aus CSV File f = new File(WvkImporter.BASEDIR, FILE_REVIERE); if (f.exists()) { try (InputStream in = new FileInputStream(f)) { ICsvListReader csv = new CsvListReader(new InputStreamReader(in, "UTF-8"), prefs); for (List<String> l = csv.read(); l != null; l = csv.read()) { final String[] line = l.toArray(new String[l.size()]); String id = line[0]; if (!StringUtils.isNumeric(id)) { System.err.println("Skipping header line: " + Arrays.toString(line)); continue; }//w ww . j ava 2 s.c o m String gmkSchl = line[0]; String gemarkung = line[1]; String gemeinde = line[2]; String key = gemeinde + "/" + gemarkung; neueGmks.put(key, gmkSchl); } } } else { System.err.println("Keine " + FILE_REVIERE + "! Import Modus?"); neueGmks = null; } }
From source file:org.polymap.wbv.mdb.GmkImporter.java
public GmkImporter() throws IOException { CsvPreference prefs = new CsvPreference('"', ',', "\r\n"); // quoteChar, delimiterChar, endOfLineSymbols // gmkMapping try (FileInputStream in = new FileInputStream(new File(WvkImporter.BASEDIR, FILE_GEMARKUNGEN))) { CsvListReader csv = new CsvListReader(new InputStreamReader(in, "UTF-8"), prefs); for (List<String> l = csv.read(); l != null; l = csv.read()) { final String[] line = l.toArray(new String[l.size()]); String id = line[2];//from w w w . j a v a 2 s . c om if (!StringUtils.isNumeric(id)) { System.err.println("Skipping header line: " + Arrays.toString(line)); continue; } String gemeinde = line[2]; String gemarkung = line[3]; String gmkschl = line[4]; Pair key = Pair.of(gemeinde, gemarkung); gmkMapping.put(key, gmkschl); } } }
From source file:org.polymap.wbv.ui.AdminPanel.java
protected void createGemarkungSection(Composite parent) { IPanelToolkit tk = getSite().toolkit(); IPanelSection section = tk.createPanelSection(parent, "Gemarkungen/Forstreviere: Import CSV-Daten"); section.addConstraint(new PriorityConstraint(100), WbvPlugin.MIN_COLUMN_WIDTH); // section.getBody().setData( WidgetUtil.CUSTOM_VARIANT, DesktopToolkit.CSS_FORM ); tk.createFlowText(section.getBody(), "Import einer **CSV-Datei** mit Stammdaten der Gemarkungen/Gemeinden." + " Der Import startet sofort nach der Auswahl der Datei. Die bisherigen Eintrge werden dabei **gelscht**!" + "\n\nDie CSV-Datei muss im **Zeichensatz UTF-8** kodiert sein und folgende **Spalten** in der folgenden Reihenfolge enthalten:" + "\n\n* Gemarkungsschlssel" + "\n* Gemarkung" + "\n* Gemeinde" + "\n* Forstrevier") .setLayoutData(new ConstraintData(new PriorityConstraint(1))); IPanelSection formSection = tk.createPanelSection(section, null); formSection.addConstraint(new PriorityConstraint(0)); Upload upload = new Upload(formSection.getBody(), SWT.NONE, Upload.SHOW_PROGRESS); upload.setHandler(new IUploadHandler() { @Override/*from w w w . ja va 2s.co m*/ public void uploadStarted(ClientFile clientFile, InputStream in) throws Exception { // quoteChar, delimiterChar, endOfLineSymbols CsvPreference prefs = new CsvPreference('"', ',', "\r\n"); ICsvListReader csv = new CsvListReader(new InputStreamReader(in, "UTF-8"), prefs); try (UnitOfWork uow = WbvRepository.newUnitOfWork();) { // aktuelle Gemarkung Entities lschen for (Gemarkung gmk : uow.query(Gemarkung.class).execute()) { uow.removeEntity(gmk); } uow.commit(); // neue importieren int count = 0; for (List<String> l = csv.read(); l != null; l = csv.read(), count++) { final String[] line = l.toArray(new String[l.size()]); String id = line[0]; if (!StringUtils.isNumeric(id)) { log.warn("Skipping header line: " + Arrays.toString(line)); continue; } uow.createEntity(Gemarkung.class, id, new ValueInitializer<Gemarkung>() { @Override public Gemarkung initialize(Gemarkung proto) throws Exception { proto.gemarkung.set(line[1]); proto.gemeinde.set(line[2]); proto.revier.set(line[3]); log.debug(proto); return proto; } }); Revier.all.clear(); } log.info("IMPORT: CSV lines: " + count + ", now in store: " + uow.query(Gemarkung.class).execute().size()); uow.commit(); log.info("IMPORT: now in store: " + uow.query(Gemarkung.class).execute().size()); } catch (Exception e) { StatusDispatcher.handleError("Die Daten konnten nicht korrekt importiert werden.", e); } } }); }