private void importActivity(JSONObject activity, String portal_image_url) { // System.out.println(activity.toString()); try { JSONObject member = activity.getJSONObject("member"); if (member.containsKey("id")) { String title = activity.getString("body").replace(activity.getString("image_large_url"), "").trim(); DBObject photo = new BasicDBObject(); photo.put("portal_image_url", portal_image_url); if (mPhotoCollection.count(photo) > 0) { System.err.println(portal_image_url + " already exists"); return; } else { System.out.println("Inserting " + portal_image_url); } String imageSourceUrl = Config.PORTAL_URL.startsWith("http:") ? portal_image_url.replace("https://", "http://") : portal_image_url; BufferedImage[] images = ImageUtil.convertImage(new URL(imageSourceUrl)); if (images == null) { System.err.println(portal_image_url + " corrupted"); return; } saveThumbnailImages(photo, images, "/" + getFileId(portal_image_url) + ".png"); photo.put("uploader_user_id", member.getString("id")); photo.put("title", title); Object exifObj = getExifData(portal_image_url); Date date = null; if (exifObj instanceof JSONObject) { JSONObject exif = (JSONObject) exifObj; if (exif.containsKey("date")) { try { date = FORMAT_DATE_ISO.parse(exif.getString("date")); } catch (ParseException e) { e.printStackTrace(); } } if (exif.containsKey("location")) { photo.put("exif_location", exif.getJSONArray("location")); } } if (date == null && activity.containsKey("created_at")) { try { date = FORMAT_DATE.parse(activity.getString("created_at")); } catch (ParseException e) { e.printStackTrace(); } } if (date != null) { photo.put("exif_date", date); } mPhotoCollection.insert(photo); // System.out.println(photo); // System.out.println(); } } catch (JSONException | MalformedURLException e) { e.printStackTrace(); } }
private static String getImageURL(JSONObject activity) { String result = null; try { Object image_url = activity.get("image_large_url"); if (image_url instanceof String && !image_url.toString().endsWith("/no_image.gif") && activity.containsKey("member")) { result = (String) image_url; if (!result.matches("^https?://.*")) { result = Config.PORTAL_ROOT + result; } } } catch (Exception e) { e.printStackTrace(); } return result; }
protected boolean validateValue( Class<?> beanClazz, String name, JSONObject jsonObj, ValidationException validationException) throws JSONException { boolean rightValue = true; try { Field field = beanClazz.getDeclaredField(name); Annotation[] annotations = field.getDeclaredAnnotations(); ArrayList<Annotation> validators = new ArrayList<Annotation>(); boolean inputRequired = false; for (Annotation annotation : annotations) { if (AnnotationValidator.isValidationAnnotation(annotation)) { validators.add(annotation); } if (!inputRequired) { if (annotation instanceof com.maxiaohua.genealogy.fw.core.validator.type.NotNull || annotation instanceof com.maxiaohua.genealogy.fw.core.validator.type.NotEmpty) { inputRequired = true; } else if (annotation instanceof MultiField) { MultiField multiField = (MultiField) annotation; if (multiField .validator() .equals(com.maxiaohua.genealogy.fw.core.validator.type.NotAllEmpty.class)) { inputRequired = true; } } } } boolean sitei = jsonObj.containsKey(name); if (inputRequired || (sitei && jsonObj.get(name) != null)) { Collections.sort( validators, new Comparator<Annotation>() { public int compare(Annotation arg0, Annotation arg1) { int result = -1; try { Integer junban0 = (Integer) arg0.getClass().getDeclaredMethod(KEY_JUNBAN).invoke(arg0); Integer junban2 = (Integer) arg1.getClass().getDeclaredMethod(KEY_JUNBAN).invoke(arg1); result = junban0.compareTo(junban2); } catch (NoSuchMethodException nsme) { errorLogger.writeErrorLog(nsme.getMessage()); appLogger.error(nsme.getMessage(), nsme); debugLogger.error(nsme.getMessage(), nsme); } catch (InvocationTargetException ite) { errorLogger.writeErrorLog(ite.getMessage()); appLogger.error(ite.getMessage(), ite); debugLogger.error(ite.getMessage(), ite); } catch (IllegalAccessException iae) { errorLogger.writeErrorLog(iae.getMessage()); appLogger.error(iae.getMessage(), iae); debugLogger.error(iae.getMessage(), iae); } return result; } }); for (Annotation annotation : validators) { if (annotation instanceof MultiField) { MultiField multiField = (MultiField) annotation; String[] fieldNames = multiField.others(); if (fieldNames != null) { Object[] values = new Object[fieldNames.length + 1]; if (sitei) { values[0] = jsonObj.get(name); } for (int i = 0; i < fieldNames.length; i++) { if (jsonObj.containsKey(fieldNames[i])) { values[i + 1] = jsonObj.get(fieldNames[i]); } } AnnotationValidator validator = AnnotationValidationUtil.validate(values, annotation); if (validator != null) { String errorCode = validator.getErrorCode(); String fieldBungen = beanClazz.getName() + "." + name; Alias alias = (Alias) field.getAnnotation(Alias.class); if (alias != null) { fieldBungen = alias.value(); } String[] parameters = null; String[] ps = validator.getMsgParameters(); if (ps == null) { parameters = new String[2]; parameters[0] = fieldBungen; StringBuffer othersBungen = new StringBuffer(); for (int i = 0; i < fieldNames.length; i++) { try { String bungen = beanClazz.getName() + "." + name; Field f = beanClazz.getDeclaredField(fieldNames[i]); Alias a = (Alias) f.getAnnotation(Alias.class); if (a != null) { bungen = a.value(); } if (i == 0) { othersBungen.append(bungen); } else { othersBungen.append(",").append(bungen); } } catch (NoSuchFieldException nsfe) { nsfe.printStackTrace(); } } parameters[1] = othersBungen.toString(); } else { parameters = new String[ps.length + 1]; parameters[0] = fieldBungen; for (int i = 0; i < ps.length; i++) { parameters[i + 1] = ps[i]; } } validationException.addValidationException(errorCode, parameters, null); rightValue = false; } } } else { Object value = null; if (sitei) { value = jsonObj.get(name); } AnnotationValidator validator = AnnotationValidationUtil.validate(value, annotation); if (validator != null) { String errorCode = validator.getErrorCode(); String fieldBungen = beanClazz.getName() + "." + name; Alias alias = (Alias) field.getAnnotation(Alias.class); if (alias != null) { fieldBungen = alias.value(); } String[] parameters = null; String[] ps = validator.getMsgParameters(); if (ps == null) { parameters = new String[] {fieldBungen}; } else { parameters = new String[ps.length + 1]; parameters[0] = fieldBungen; for (int i = 0; i < ps.length; i++) { parameters[i + 1] = ps[i]; } } validationException.addValidationException(errorCode, parameters, null); rightValue = false; } } if (!rightValue) { break; } } } } catch (NoSuchFieldException nsfe) { } return rightValue; }
protected Object parseBean( JSONObject jsonObj, Class<?> beanClazz, ValidationException validationException) throws IntrospectionException, IllegalAccessException, InstantiationException, JSONException, InvocationTargetException, ParseException { if (beanClazz == null) { throw new AJAXParserException("No specific class type."); } if (jsonObj == null) { return null; } BeanInfo info = Introspector.getBeanInfo(beanClazz); PropertyDescriptor[] propDescriptors = info.getPropertyDescriptors(); Object instance = beanClazz.newInstance(); for (PropertyDescriptor property : propDescriptors) { String name = property.getName(); Class<?> propClazz = property.getPropertyType(); if (Class.class.equals(propClazz)) { continue; } Method setter = property.getWriteMethod(); if (setter == null) { continue; } if ((propClazz.equals(java.sql.Date.class) || propClazz.equals(java.sql.Time.class) || propClazz.equals(java.sql.Timestamp.class) || propClazz.equals(java.util.Date.class)) && jsonObj.containsKey(name)) { Object tempValue = jsonObj.get(name); if (tempValue != null && StringUtil.isEmpty(tempValue.toString())) { tempValue = null; jsonObj.put(name, tempValue); } } if (validateValue(beanClazz, name, jsonObj, validationException) && jsonObj.containsKey(name)) { Object nestObj = null; if (isServiceBeanType(propClazz)) { JSONObject jsonObjPropValue = jsonObj.getJSONObject(name); nestObj = parseBean(jsonObjPropValue, propClazz, validationException); } else if (isBasicType(propClazz)) { Object jsonObjPropValue = jsonObj.get(name); nestObj = parseBasic(jsonObjPropValue == null ? null : jsonObjPropValue.toString(), propClazz); } else if (propClazz.isArray()) { if (jsonObj.get(name) == null) { continue; } JSONArray jsonArrPropValue = jsonObj.getJSONArray(name); nestObj = parseArray(jsonArrPropValue, propClazz.getComponentType(), validationException); } else if (List.class.isAssignableFrom(propClazz)) { if (jsonObj.get(name) == null) { continue; } Type genericType = property.getReadMethod().getGenericReturnType(); JSONArray jsonArrPropValue = jsonObj.getJSONArray(name); nestObj = parseList(jsonArrPropValue, propClazz, genericType, validationException); } else if (Map.class.isAssignableFrom(propClazz)) { Type genericType = property.getReadMethod().getGenericReturnType(); JSONObject jsonObjPropValue = jsonObj.getJSONObject(name); nestObj = parseMap(jsonObjPropValue, propClazz, genericType, validationException); } else if (java.lang.Object.class.equals(propClazz)) { nestObj = jsonObj.getJSONObject(name); } else { nestObj = null; } setter.invoke(instance, new Object[] {nestObj}); } } return instance; }
public void settingFromJSON(JSONObject json) { jsonParams = json; try { JSONObject ldplParams = json.getJSONObject(LDPLParameters); JSONObject gpParams = json.getJSONObject(GPParameters); JSONObject optJSON = json.getJSONObject(OPTIONS); // LDPLParameters double n = ldplParams.getDouble("n"); double A = ldplParams.getDouble("A"); double fa = ldplParams.getDouble("fa"); double fb = ldplParams.getDouble("fb"); if (ldplParams.containsKey(HDIFF)) { double hDiff = ldplParams.getDouble(HDIFF); this.setParams(new double[] {n, A, fa, fb}); this.setHDiff(hDiff); } // GPParamters JSONArray jarray = gpParams.getJSONArray("lengthes"); lengthes = JSONUtils.array1DfromJSONArray(jarray); stdev = gpParams.getDouble("sigmaP"); sigmaN = gpParams.getDouble("sigmaN"); boolean useMask = (gpParams.getInt("useMask") == 1); int optConstVar = gpParams.getInt("constVar"); gpLDPL.setUseMask(useMask); gpLDPL.setOptConstVar(optConstVar); // Options doHyperParameterOptimize = optJSON.optBoolean("optimizeHyperParameters"); if (!doHyperParameterOptimize) { doHyperParameterOptimize = optJSON.optInt("optimizeHyperParameters") == 1; } // Optional variables if (gpParams.containsKey(ARRAYS)) { JSONObject jobjArrays = gpParams.getJSONObject(ARRAYS); gpLDPL.fromJSON(jobjArrays); } if (json.containsKey(LIKELIHOOD_MODEL)) { JSONObject likelihoodJSON = json.getJSONObject(LIKELIHOOD_MODEL); LikelihoodModel likelihoodModel = LikelihoodModelFactory.create(likelihoodJSON); gpLDPL.setLikelihoodModel(likelihoodModel); } else { System.out.println("The key for [" + LIKELIHOOD_MODEL + "] was not found."); } if (json.containsKey(BEACON_FILTER)) { JSONObject bfJSON = json.getJSONObject(BEACON_FILTER); BeaconFilter bf = BeaconFilterFactory.create(bfJSON); beaconFilter = bf; } else { System.out.println("The key for [" + BEACON_FILTER + "] was not found."); } } catch (JSONException e) { e.printStackTrace(); } }