List of usage examples for org.apache.commons.beanutils PropertyUtils setProperty
public static void setProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
Set the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.
For more details see PropertyUtilsBean
.
From source file:org.ms123.common.data.MultiOperations.java
public static void populate(SessionContext sessionContext, Map sourceMap, Object destinationObj, Map hintsMap) { PersistenceManager pm = sessionContext.getPM(); if (hintsMap == null) { hintsMap = new HashMap(); }//from w ww . ja va 2 s . c o m boolean noUpdate = Utils.getBoolean(hintsMap, "noUpdate", false); BeanMap destinationMap = new BeanMap(destinationObj); String entityName = m_inflector.getEntityName(destinationObj.getClass().getSimpleName()); debug("populate.sourceMap:" + sourceMap + ",destinationObj:" + destinationObj + ",destinationMap:" + destinationMap + "/hintsMap:" + hintsMap + "/entityName:" + entityName); if (sourceMap == null) { return; } debug("populate(" + entityName + ") is a persistObject:" + javax.jdo.JDOHelper.isPersistent(destinationObj) + "/" + javax.jdo.JDOHelper.isNew(destinationObj)); if (sourceMap.get("id") != null) { debug("populate(" + entityName + ") has id:" + sourceMap.get("id")); return; } Map permittedFields = sessionContext.getPermittedFields(entityName, "write"); Iterator<String> it = sourceMap.keySet().iterator(); while (it.hasNext()) { String propertyName = it.next(); boolean permitted = sessionContext.getPermissionService().hasAdminRole() || "team".equals(entityName) || sessionContext.isFieldPermitted(propertyName, entityName, "write"); if (!propertyName.startsWith("_") && !permitted) { debug("---->populate:field(" + propertyName + ") no write permission"); continue; } else { debug("++++>populate:field(" + propertyName + ") write permitted"); } String datatype = null; String edittype = null; if (!propertyName.startsWith("_")) { Map config = (Map) permittedFields.get(propertyName); if (config != null) { datatype = (String) config.get("datatype"); edittype = (String) config.get("edittype"); } } if (propertyName.equals(STATE_FIELD) && !sessionContext.getPermissionService().hasAdminRole()) { continue; } if ("auto".equals(edittype)) continue; String mode = null; Map hm = (Map) hintsMap.get(propertyName); if (hm != null) { Object m = hm.get("mode"); if (m != null && m instanceof String) { mode = (String) m; } if (mode == null) { m = hm.get("useit"); if (m != null && m instanceof String) { mode = (String) m; } } } if (mode == null) { mode = "replace"; } Class propertyClass = destinationMap.getType(propertyName); debug("\ttype:" + propertyClass + "(" + propertyName + "=" + sourceMap.get(propertyName) + ")"); if ("_ignore_".equals(sourceMap.get(propertyName))) { continue; } if (propertyClass == null) { debug("\t--- Warning property not found:" + propertyName); } else if (propertyClass.equals(java.util.Date.class)) { String value = Utils.getString(sourceMap.get(propertyName), destinationMap.get(propertyName), mode); debug("\tDate found:" + propertyName + "=>" + value); Date date = null; if (value != null) { try { Long val = Long.valueOf(value); date = (Date) ConvertUtils.convert(val, Date.class); debug("\tdate1:" + date); } catch (Exception e) { try { DateTime dt = new DateTime(value); date = new Date(dt.getMillis()); debug("\tdate2:" + date); } catch (Exception e1) { try { int space = value.indexOf(" "); if (space != -1) { value = value.substring(0, space) + "T" + value.substring(space + 1); DateTime dt = new DateTime(value); date = new Date(dt.getMillis()); } debug("\tdate3:" + date); } catch (Exception e2) { debug("\terror setting date:" + e); } } } } debug("\tsetting date:" + date); destinationMap.put(propertyName, date); } else if (propertyClass.equals(java.util.Map.class)) { info("!!!!!!!!!!!!!!!!!!!Map not implemented"); } else if (propertyClass.equals(java.util.List.class) || propertyClass.equals(java.util.Set.class)) { boolean isList = propertyClass.equals(java.util.List.class); boolean isSimple = false; if (datatype != null && datatype.startsWith("list_")) { isSimple = true; } try { Class propertyType = TypeUtils.getTypeForField(destinationObj, propertyName); debug("propertyType:" + propertyType + " fill with: " + sourceMap.get(propertyName) + ",list:" + destinationMap.get(propertyName) + "/mode:" + mode); Collection sourceList = isList ? new ArrayList() : new HashSet(); Object fromVal = sourceMap.get(propertyName); if (fromVal instanceof String && ((String) fromVal).length() > 0) { info("FromVal is StringSchrott, ignore"); continue; } if (sourceMap.get(propertyName) instanceof Collection) { sourceList = (Collection) sourceMap.get(propertyName); } if (sourceList == null) { sourceList = isList ? new ArrayList() : new HashSet(); } Collection destinationList = (Collection) PropertyUtils.getProperty(destinationObj, propertyName); debug("destinationList:" + destinationList); debug("sourceList:" + sourceList); if (destinationList == null) { destinationList = isList ? new ArrayList() : new HashSet(); PropertyUtils.setProperty(destinationObj, propertyName, destinationList); } if ("replace".equals(mode)) { boolean isEqual = false; if (isSimple) { isEqual = Utils.isCollectionEqual(destinationList, sourceList); if (!isEqual) { destinationList.clear(); } debug("\tisEqual:" + isEqual); } else { List deleteList = new ArrayList(); String namespace = sessionContext.getStoreDesc().getNamespace(); for (Object o : destinationList) { if (propertyType.getName().endsWith(".Team")) { int status = sessionContext.getTeamService().getTeamStatus(namespace, new BeanMap(o), null, sessionContext.getUserName()); debug("populate.replace.teamStatus:" + status + "/" + new HashMap(new BeanMap(o))); if (status != -1) { pm.deletePersistent(o); deleteList.add(o); } } else { pm.deletePersistent(o); deleteList.add(o); } } for (Object o : deleteList) { destinationList.remove(o); } } debug("populate.replace.destinationList:" + destinationList + "/" + propertyType.getName()); if (isSimple) { if (!isEqual) { for (Object o : sourceList) { destinationList.add(o); } } } else { for (Object o : sourceList) { Map childSourceMap = (Map) o; Object childDestinationObj = propertyType.newInstance(); if (propertyType.getName().endsWith(".Team")) { childSourceMap.remove("id"); Object desc = childSourceMap.get("description"); Object name = childSourceMap.get("name"); Object dis = childSourceMap.get("disabled"); String teamid = (String) childSourceMap.get("teamid"); Object ti = Utils.getTeamintern(sessionContext, teamid); if (desc == null) { childSourceMap.put("description", PropertyUtils.getProperty(ti, "description")); } if (name == null) { childSourceMap.put("name", PropertyUtils.getProperty(ti, "name")); } if (dis == null) { childSourceMap.put("disabled", false); } pm.makePersistent(childDestinationObj); populate(sessionContext, childSourceMap, childDestinationObj, hintsMap); PropertyUtils.setProperty(childDestinationObj, "teamintern", ti); } else { pm.makePersistent(childDestinationObj); populate(sessionContext, childSourceMap, childDestinationObj, hintsMap); } debug("populated.add:" + new HashMap(new BeanMap(childDestinationObj))); destinationList.add(childDestinationObj); } } } else if ("remove".equals(mode)) { if (isSimple) { for (Object o : sourceList) { if (destinationList.contains(o)) { destinationList.remove(o); } } } else { for (Object ol : sourceList) { Map childSourceMap = (Map) ol; Object o = Utils.listContainsId(destinationList, childSourceMap, "teamid"); if (o != null) { destinationList.remove(o); pm.deletePersistent(o); } } } } else if ("add".equals(mode)) { if (isSimple) { for (Object o : sourceList) { destinationList.add(o); } } else { for (Object ol : sourceList) { Map childSourceMap = (Map) ol; Object childDestinationObj = Utils.listContainsId(destinationList, childSourceMap, "teamid"); if (childDestinationObj != null) { populate(sessionContext, childSourceMap, childDestinationObj, hintsMap); } else { childDestinationObj = propertyType.newInstance(); if (propertyType.getName().endsWith(".Team")) { Object desc = childSourceMap.get("description"); Object name = childSourceMap.get("name"); Object dis = childSourceMap.get("disabled"); String teamid = (String) childSourceMap.get("teamid"); Object ti = Utils.getTeamintern(sessionContext, teamid); if (desc == null) { childSourceMap.put("description", PropertyUtils.getProperty(ti, "description")); } if (name == null) { childSourceMap.put("name", PropertyUtils.getProperty(ti, "name")); } if (dis == null) { childSourceMap.put("disabled", false); } pm.makePersistent(childDestinationObj); populate(sessionContext, childSourceMap, childDestinationObj, hintsMap); PropertyUtils.setProperty(childDestinationObj, "teamintern", ti); } else { pm.makePersistent(childDestinationObj); populate(sessionContext, childSourceMap, childDestinationObj, hintsMap); } destinationList.add(childDestinationObj); } } } } else if ("assign".equals(mode)) { if (!isSimple) { for (Object ol : sourceList) { Map childSourceMap = (Map) ol; Object childDestinationObj = Utils.listContainsId(destinationList, childSourceMap); if (childDestinationObj != null) { debug("id:" + childSourceMap + " already assigned"); } else { Object id = childSourceMap.get("id"); Boolean assign = Utils.getBoolean(childSourceMap.get("assign")); Object obj = pm.getObjectById(propertyType, id); if (assign) { destinationList.add(obj); } else { destinationList.remove(obj); } } } } } } catch (Exception e) { e.printStackTrace(); debug("populate.list.failed:" + propertyName + "=>" + sourceMap.get(propertyName) + ";" + e); } } else if (propertyClass.equals(java.lang.Boolean.class)) { try { destinationMap.put(propertyName, ConvertUtils.convert(sourceMap.get(propertyName), Boolean.class)); } catch (Exception e) { debug("populate.boolean.failed:" + propertyName + "=>" + sourceMap.get(propertyName) + ";" + e); } } else if (propertyClass.equals(java.lang.Double.class)) { String value = Utils.getString(sourceMap.get(propertyName), destinationMap.get(propertyName), mode); try { destinationMap.put(propertyName, Double.valueOf(value)); } catch (Exception e) { debug("populate.double.failed:" + propertyName + "=>" + value + ";" + e); } } else if (propertyClass.equals(java.lang.Long.class)) { try { destinationMap.put(propertyName, ConvertUtils.convert(sourceMap.get(propertyName), Long.class)); } catch (Exception e) { debug("populate.long.failed:" + propertyName + "=>" + sourceMap.get(propertyName) + ";" + e); } } else if (propertyClass.equals(java.lang.Integer.class)) { debug("Integer:" + ConvertUtils.convert(sourceMap.get(propertyName), Integer.class)); try { destinationMap.put(propertyName, ConvertUtils.convert(sourceMap.get(propertyName), Integer.class)); } catch (Exception e) { debug("populate.integer.failed:" + propertyName + "=>" + sourceMap.get(propertyName) + ";" + e); } } else if ("binary".equals(datatype) || propertyClass.equals(byte[].class)) { InputStream is = null; InputStream is2 = null; try { if (sourceMap.get(propertyName) instanceof FileItem) { FileItem fi = (FileItem) sourceMap.get(propertyName); String name = fi.getName(); byte[] bytes = IOUtils.toByteArray(fi.getInputStream()); if (bytes != null) { debug("bytes:" + bytes.length); } destinationMap.put(propertyName, bytes); is = fi.getInputStream(); is2 = fi.getInputStream(); } else if (sourceMap.get(propertyName) instanceof Map) { Map map = (Map) sourceMap.get(propertyName); String storeLocation = (String) map.get("storeLocation"); is = new FileInputStream(new File(storeLocation)); is2 = new FileInputStream(new File(storeLocation)); byte[] bytes = IOUtils.toByteArray(is); if (bytes != null) { debug("bytes2:" + bytes.length); } is.close(); destinationMap.put(propertyName, bytes); is = new FileInputStream(new File(storeLocation)); } else if (sourceMap.get(propertyName) instanceof String) { String value = (String) sourceMap.get(propertyName); if (value.startsWith("data:")) { int ind = value.indexOf(";base64,"); byte b[] = Base64.decode(value.substring(ind + 8)); destinationMap.put(propertyName, b); is = new ByteArrayInputStream(b); is2 = new ByteArrayInputStream(b); } else { } } else { debug("populate.byte[].no a FileItem:" + propertyName + "=>" + sourceMap.get(propertyName)); continue; } Tika tika = new Tika(); TikaInputStream stream = TikaInputStream.get(is); TikaInputStream stream2 = TikaInputStream.get(is2); String text = tika.parseToString(is); debug("Text:" + text); try { destinationMap.put("text", text); } catch (Exception e) { destinationMap.put("text", text.getBytes()); } //@@@MS Hardcoded try { Detector detector = new DefaultDetector(); MediaType mime = detector.detect(stream2, new Metadata()); debug("Mime:" + mime.getType() + "|" + mime.getSubtype() + "|" + mime.toString()); destinationMap.put("type", mime.toString()); sourceMap.put("type", mime.toString()); } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); debug("populate.byte[].failed:" + propertyName + "=>" + sourceMap.get(propertyName) + ";" + e); } finally { try { is.close(); is2.close(); } catch (Exception e) { } } } else { boolean ok = false; try { Class propertyType = TypeUtils.getTypeForField(destinationObj, propertyName); debug("propertyType:" + propertyType + "/" + propertyName); if (propertyType != null) { boolean hasAnn = propertyType.isAnnotationPresent(PersistenceCapable.class); debug("hasAnnotation:" + hasAnn); if (propertyType.newInstance() instanceof javax.jdo.spi.PersistenceCapable || hasAnn) { handleRelatedTo(sessionContext, sourceMap, propertyName, destinationMap, destinationObj, propertyType); Object obj = sourceMap.get(propertyName); if (obj != null && obj instanceof Map) { Map childSourceMap = (Map) obj; Object childDestinationObj = destinationMap.get(propertyName); if (childDestinationObj == null) { childDestinationObj = propertyType.newInstance(); destinationMap.put(propertyName, childDestinationObj); } populate(sessionContext, childSourceMap, childDestinationObj, hintsMap); } else { if (obj == null) { destinationMap.put(propertyName, null); } } ok = true; } } } catch (Exception e) { e.printStackTrace(); } if (!ok) { String value = Utils.getString(sourceMap.get(propertyName), destinationMap.get(propertyName), mode); try { if (noUpdate) { if (Utils.isEmptyObj(destinationMap.get(propertyName))) { destinationMap.put(propertyName, value); } } else { destinationMap.put(propertyName, value); } } catch (Exception e) { debug("populate.failed:" + propertyName + "=>" + value + ";" + e); } } } } }
From source file:org.ms123.common.data.quality.QualityBatch.java
private Object compareToOKList(SessionContext sc, Object candidate, boolean dry) throws Exception { reset();//from www .j ava2s . co m info("record.num:" + m_count++); Object candId = getProperty(candidate, ID); String entityName = getEntityName(); Class clazz = sc.getClass(entityName); String filter = STATE_FIELD + " == \"" + STATE_OK + "\""; Extent e = sc.getPM().getExtent(clazz, true); Query q = sc.getPM().newQuery(e, filter); q.addExtension("datanucleus.rdbms.query.resultSetConcurrency", "read-only"); q.addExtension("datanucleus.rdbms.query.fetchDirection", "forward"); q.declareImports(m_sdesc.getImports()); try { Collection coll = (Collection) q.execute(); Iterator iter = coll.iterator(); while (iter.hasNext()) { Object refObj = iter.next(); if (candId != null) { Object refId = getProperty(refObj, ID); if (candId.equals(refId)) { continue; } } boolean b = compareOne(refObj, candidate); if (b == true) { m_dups++; if (!dry) { String refid = (String) PropertyUtils.getProperty(refObj, ID); PropertyUtils.setProperty(candidate, STATE_FIELD, STATE_DUP); PropertyUtils.setProperty(candidate, STATE_REFID, refid); } return refObj; } } } finally { q.closeAll(); } if (!dry) { PropertyUtils.setProperty(candidate, STATE_FIELD, STATE_OK); } return null; }
From source file:org.ms123.common.data.TriggerServiceImpl.java
public Map applyDeleteRules(SessionContext sessionContext, String entityName, Object insert) throws Exception { // Map preUpdate = new HashMap(new BeanMap(insert)); Map preUpdate = UtilsServiceImpl.copyObject(insert); if (PropertyUtils.isReadable(insert, "_team_list")) { PropertyUtils.setProperty(insert, "_team_list", new HashSet()); }/*ww w. j a va 2s .c om*/ return applyRules(sessionContext, entityName, insert, preUpdate, DELETE); }
From source file:org.ms123.common.docbook.SwebleDocbook.java
private void setAttribute(Object o, String attr, String value) { try {/* w w w . j a v a 2 s . co m*/ PropertyUtils.setProperty(o, attr, value); } catch (Exception e) { e.printStackTrace(); } }
From source file:org.ms123.common.docbook.WebsiteBuilder.java
private void setAttribute(Object o, String attr, String value) { try {// w w w . j a v a 2 s. c om if (o instanceof Map) { ((Map) o).put(attr, value); } else { PropertyUtils.setProperty(o, attr, value); } } catch (Exception e) { e.printStackTrace(); } }
From source file:org.ms123.common.ea.BaseEAServiceImpl.java
private Map importActivities(String storeId, String basedir) throws Exception { String json = readFileToString(new File(basedir, "idmap.map")); Map<String, String> idmap = (Map) m_ds.deserialize(json); Calendar high_cal = Calendar.getInstance(); high_cal.set(Calendar.YEAR, 2050); high_cal.set(Calendar.MONTH, 11); high_cal.set(Calendar.DAY_OF_MONTH, 31); Calendar low_cal = Calendar.getInstance(); low_cal.set(Calendar.YEAR, 2012); low_cal.set(Calendar.MONTH, 11); low_cal.set(Calendar.DAY_OF_MONTH, 12); StoreDesc sdesc = StoreDesc.get(storeId); PersistenceManager pm = m_nucleusService.getPersistenceManagerFactory(sdesc).getPersistenceManager(); UserTransaction ut = m_nucleusService.getUserTransaction(); Map mapping = initActivities(); try {/*www .j ava 2 s .c o m*/ LabeledCSVParser lp = new LabeledCSVParser( new ExcelCSVParser(new FileInputStream(new File(basedir, "Kontakte.csv")))); System.out.println("Persisting activities"); int num = 0; Class _contact = m_nucleusService.getClass(sdesc, "Contact"); Class _teamIntern = m_nucleusService.getClass(sdesc, "Teamintern"); while (lp.getLine() != null) { String nummer = lp.getValueByLabel("Nummer"); String merkmal = lp.getValueByLabel("Merkmal"); Object c = getObject(pm, _contact, nummer); if (c == null) { continue; } if (ut.getStatus() != Status.STATUS_ACTIVE) { ut.begin(); } String teamid = idmap.get(merkmal); Object activity = m_nucleusService.getClass(sdesc, "Activity").newInstance(); if (teamid != null) { Object team = m_nucleusService.getClass(sdesc, "Team").newInstance(); Object teamintern = getTeamintern(pm, _teamIntern, teamid); PropertyUtils.setProperty(team, "teamintern", teamintern); PropertyUtils.setProperty(team, "teamid", teamid); PropertyUtils.setProperty(team, "description", PropertyUtils.getProperty(teamintern, "description")); PropertyUtils.setProperty(team, "validFrom", low_cal.getTime()); PropertyUtils.setProperty(team, "validTo", high_cal.getTime()); PropertyUtils.setProperty(team, "disabled", false); Collection l = (Collection) PropertyUtils.getProperty(activity, "_team_list"); if (l == null) { l = new HashSet(); PropertyUtils.setProperty(activity, "_team_list", l); } l.add(team); } Iterator it = mapping.keySet().iterator(); while (it.hasNext()) { String key = (String) it.next(); if (key.equals("traits")) { continue; } String[] m1 = (String[]) mapping.get(key); String field = m1[0]; String type = m1[1]; String val = lp.getValueByLabel(key).trim(); if (type.equals("date")) { Date d = getDate(val); if (d != null) { BeanUtils.setProperty(activity, field, d); } } else if (type.equals("boolean")) { Boolean b = false; if ("J".equals(val)) { b = true; } BeanUtils.setProperty(activity, field, b); } else { if (val != null) { BeanUtils.setProperty(activity, field, val); } } } Collection l = (Collection) PropertyUtils.getProperty(c, "activity_list"); l.add(activity); PropertyUtils.setProperty(activity, "contact", c); pm.makePersistent(activity); //LuceneSession luceneSession = m_luceneService.createSession(sdesc); //m_luceneService.addToIndex(luceneSession, activity); //m_luceneService.commit(luceneSession); if ((num % 1000) == 1) { System.out.println(num + ":\t" + new Date().getTime()); ut.commit(); } num++; } if (ut.getStatus() == Status.STATUS_ACTIVE) { ut.commit(); } System.out.println("Contact and Book have been persisted"); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } finally { pm.close(); } return null; }
From source file:org.ms123.common.ea.BaseEAServiceImpl.java
private Map importTeams(String storeId, String basedir) throws Exception { Calendar high_cal = Calendar.getInstance(); high_cal.set(Calendar.YEAR, 2050); high_cal.set(Calendar.MONTH, 11); high_cal.set(Calendar.DAY_OF_MONTH, 31); Calendar low_cal = Calendar.getInstance(); low_cal.set(Calendar.YEAR, 2012); low_cal.set(Calendar.MONTH, 11); low_cal.set(Calendar.DAY_OF_MONTH, 12); String json = readFileToString(new File(basedir, "idmap.map")); Map<String, String> idmap = (Map) m_ds.deserialize(json); StoreDesc sdesc = StoreDesc.get(storeId); PersistenceManager pm = m_nucleusService.getPersistenceManagerFactory(sdesc).getPersistenceManager(); UserTransaction ut = m_nucleusService.getUserTransaction(); try {/*from w ww . j a v a 2 s . c o m*/ LabeledCSVParser lp = new LabeledCSVParser( new ExcelCSVParser(new FileInputStream(new File(basedir, "Merkmale.csv")))); System.out.println("Persisting teams"); int num = 0; Class _contact = m_nucleusService.getClass(sdesc, "Contact"); Class _company = m_nucleusService.getClass(sdesc, "Company"); Class _teamIntern = m_nucleusService.getClass(sdesc, "Teamintern"); while (lp.getLine() != null) { String nummer = lp.getValueByLabel("Nummer"); String merkmal = lp.getValueByLabel("Merkmal"); String beginn = lp.getValueByLabel("Beginn"); String ende = lp.getValueByLabel("Ende"); String status = lp.getValueByLabel("Status"); String teamid = idmap.get(merkmal); if (teamid == null) { System.out.println("Teamid not found:" + merkmal); continue; } Object c = getObject(pm, _contact, _company, nummer); if (c == null) { System.out.println("No contact/company:" + nummer); continue; } if (ut.getStatus() != Status.STATUS_ACTIVE) { ut.begin(); } Object team = m_nucleusService.getClass(sdesc, "Team").newInstance(); Object teamintern = getTeamintern(pm, _teamIntern, teamid); PropertyUtils.setProperty(team, "teamintern", teamintern); PropertyUtils.setProperty(team, "teamid", teamid); PropertyUtils.setProperty(team, "description", PropertyUtils.getProperty(teamintern, "description")); Boolean active = isActive(status); Date validFrom = getTeamDate(beginn); Date validTo = getTeamDate(ende); if (active != null && validFrom != null && validTo != null) { PropertyUtils.setProperty(team, "validFrom", validFrom); PropertyUtils.setProperty(team, "validTo", validTo); PropertyUtils.setProperty(team, "disabled", !active); } if (active == null && validFrom == null && validTo == null) { PropertyUtils.setProperty(team, "validFrom", low_cal.getTime()); PropertyUtils.setProperty(team, "validTo", high_cal.getTime()); PropertyUtils.setProperty(team, "disabled", false); } if (active != null && validFrom != null && validTo == null) { PropertyUtils.setProperty(team, "validFrom", validFrom); PropertyUtils.setProperty(team, "validTo", high_cal.getTime()); PropertyUtils.setProperty(team, "disabled", !active); } PropertyUtils.setProperty(team, "property1", lp.getValueByLabel("Nutzer")); PropertyUtils.setProperty(team, "property2", lp.getValueByLabel("Passwort")); Collection l = (Collection) PropertyUtils.getProperty(c, "_team_list"); if (l == null) { l = new HashSet(); PropertyUtils.setProperty(c, "_team_list", l); } l.add(team); pm.makePersistent(team); if ((num % 1000) == 1) { System.out.println(num + ":\t" + new Date().getTime()); ut.commit(); } num++; } if (ut.getStatus() == Status.STATUS_ACTIVE) { ut.commit(); } System.out.println("Contact and Book have been persisted"); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } finally { pm.close(); } return null; }
From source file:org.ms123.common.ea.BaseEAServiceImpl.java
private Map importCommunications(String storeId, String basedir) { StoreDesc sdesc = StoreDesc.get(storeId); PersistenceManager pm = m_nucleusService.getPersistenceManagerFactory(sdesc).getPersistenceManager(); UserTransaction ut = m_nucleusService.getUserTransaction(); Map mapping = initCommunication(); try {//from ww w . j av a 2 s . co m LabeledCSVParser lp = new LabeledCSVParser( new ExcelCSVParser(new FileInputStream(new File(basedir, "Kommunikation.csv")))); System.out.println("Persisting communication"); int num = 0; Class _contact = m_nucleusService.getClass(sdesc, "Contact"); Class _company = m_nucleusService.getClass(sdesc, "Company"); while (lp.getLine() != null) { String nummer = lp.getValueByLabel("Nummer"); Object c = getObject(pm, _contact, nummer); if (c == null) { c = getObject(pm, _company, nummer); if (c == null) { continue; } } if (ut.getStatus() != Status.STATUS_ACTIVE) { ut.begin(); } String typ = lp.getValueByLabel("Typ"); typ = typ.toLowerCase(); String adresse = lp.getValueByLabel("Adresse"); Object comm = PropertyUtils.getProperty(c, "communication"); if (comm == null) { comm = m_nucleusService.getClass(sdesc, "Communication").newInstance(); PropertyUtils.setProperty(c, "communication", comm); pm.makePersistent(comm); } String[] m1 = (String[]) mapping.get(typ); if (m1 == null) { System.out.println("typ(" + typ + "): not found"); continue; } String field = m1[0]; BeanUtils.setProperty(comm, field, adresse); pm.makePersistent(comm); if ((num % 1000) == 1) { System.out.println(num + ":\t" + new Date().getTime()); ut.commit(); } num++; } if (ut.getStatus() == Status.STATUS_ACTIVE) { ut.commit(); } System.out.println("Communication have been persisted"); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } finally { pm.close(); } return null; }
From source file:org.ms123.common.ea.BaseEAServiceImpl.java
private Map importZipcodes(String storeId, String basedir) { StoreDesc sdesc = StoreDesc.get(storeId); PersistenceManager pm = m_nucleusService.getPersistenceManagerFactory(sdesc).getPersistenceManager(); UserTransaction ut = m_nucleusService.getUserTransaction(); Class _company = m_nucleusService.getClass(sdesc, "Company"); Class _contact = m_nucleusService.getClass(sdesc, "Contact"); Map mapping = initZipcodes(); try {/* w w w . j ava 2 s.c om*/ LabeledCSVParser lp = new LabeledCSVParser( getCSVParser(new FileInputStream(new File(basedir, "zipcodes.csv")))); System.out.println("Persisting zipcodes"); int num = 0; while (lp.getLine() != null) { if (ut.getStatus() != Status.STATUS_ACTIVE) { ut.begin(); } // Zipcode zipcode = new Zipcode(); Object zipcode = m_nucleusService.getClass(sdesc, "Zipcode").newInstance(); String gemeindekennziffer = lp.getValueByLabel("Gemeindekennziffer"); String ortname = lp.getValueByLabel("ORTNAME"); String plz = lp.getValueByLabel("PLZ"); String lkz = lp.getValueByLabel("LKZ"); PropertyUtils.setProperty(zipcode, "lkz", lkz); PropertyUtils.setProperty(zipcode, "plz", plz); PropertyUtils.setProperty(zipcode, "ortname", ortname); PropertyUtils.setProperty(zipcode, "gemeindekennziffer", gemeindekennziffer); Collection cl = getContactList(pm, _company, plz); if (cl != null) { for (Object cx : cl) { PropertyUtils.setProperty(cx, "zipcode", zipcode); PropertyUtils.setProperty(cx, "lkz", lkz); } } cl = getContactList(pm, _contact, plz); if (cl != null) { for (Object cx : cl) { PropertyUtils.setProperty(cx, "zipcode", zipcode); PropertyUtils.setProperty(cx, "lkz", lkz); } } pm.makePersistent(zipcode); if ((num % 1000) == 1) { System.out.println(num + ":\t" + new Date().getTime()); ut.commit(); } num++; } if (ut.getStatus() == Status.STATUS_ACTIVE) { ut.commit(); } System.out.println("Contact and Book have been persisted"); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } finally { pm.close(); } return null; }
From source file:org.ms123.common.ea.EACompanyContactImporter.java
private void doImport() throws Exception { LabeledCSVParser lp = new LabeledCSVParser( new ExcelCSVParser(new FileInputStream(new File(m_basedir, "ea.csv")))); int status;/*from w ww . j a va 2 s. c o m*/ PersistenceManager pm = m_nucleusService.getPersistenceManagerFactory(m_storeDesc).getPersistenceManager(); UserTransaction ut = m_nucleusService.getUserTransaction(); int num = 0; Object company = null; String lastCompanyId = null; while (lp.getLine() != null) { String type = lp.getValueByLabel("type"); String companyId = lp.getValueByLabel("companyId"); if (type.startsWith("nok")) continue; if (ut.getStatus() != Status.STATUS_ACTIVE) { ut.begin(); } String s[] = getStateAndEntity(type); Object obj = populate(lp, s[0], s[1]); if (!isEmpty(companyId)) { if (!companyId.equals(lastCompanyId)) { company = obj; lastCompanyId = companyId; } if (s[0].equals("contact")) { Set cl = (Set) PropertyUtils.getProperty(company, "contact_list"); if (cl == null) { cl = new HashSet(); PropertyUtils.setProperty(company, "contact_list", cl); } cl.add(obj); } } pm.makePersistent(obj); if ((num % 1000) == 1) { System.out.println(num + ":\t" + new Date().getTime()); ut.commit(); } num++; } if (ut.getStatus() == Status.STATUS_ACTIVE) { ut.commit(); } }