List of usage examples for java.util Collection remove
boolean remove(Object o);
From source file:org.apache.jxtadoop.hdfs.server.namenode.FSNamesystem.java
/** * Append to an existing file in the namespace. *//*from w ww . j a v a 2 s. co m*/ LocatedBlock appendFile(String src, String holder, String clientMachine) throws IOException { if (supportAppends == false) { throw new IOException("Append to hdfs not supported." + " Please refer to dfs.support.append configuration parameter."); } startFileInternal(src, null, holder, clientMachine, false, true, (short) maxReplication, (long) 0); getEditLog().logSync(); // // Create a LocatedBlock object for the last block of the file // to be returned to the client. Return null if the file does not // have a partial block at the end. // LocatedBlock lb = null; synchronized (this) { INodeFileUnderConstruction file = (INodeFileUnderConstruction) dir.getFileINode(src); Block[] blocks = file.getBlocks(); if (blocks != null && blocks.length > 0) { Block last = blocks[blocks.length - 1]; BlockInfo storedBlock = blocksMap.getStoredBlock(last); if (file.getPreferredBlockSize() > storedBlock.getNumBytes()) { long fileLength = file.computeContentSummary().getLength(); DatanodeDescriptor[] targets = new DatanodeDescriptor[blocksMap.numNodes(last)]; Iterator<DatanodeDescriptor> it = blocksMap.nodeIterator(last); for (int i = 0; it != null && it.hasNext(); i++) { targets[i] = it.next(); } // remove the replica locations of this block from the blocksMap for (int i = 0; i < targets.length; i++) { targets[i].removeBlock(storedBlock); } // set the locations of the last block in the lease record file.setLastBlock(storedBlock, targets); lb = new LocatedBlock(last, targets, fileLength - storedBlock.getNumBytes()); // Remove block from replication queue. updateNeededReplications(last, 0, 0); // remove this block from the list of pending blocks to be deleted. // This reduces the possibility of triggering HADOOP-1349. // for (Collection<Block> v : recentInvalidateSets.values()) { if (v.remove(last)) { pendingDeletionBlocksCount--; } } } } } if (lb != null) { if (NameNode.stateChangeLog.isDebugEnabled()) { NameNode.stateChangeLog.debug("DIR* NameSystem.appendFile: file " + src + " for " + holder + " at " + clientMachine + " block " + lb.getBlock() + " block size " + lb.getBlock().getNumBytes()); } } if (auditLog.isInfoEnabled()) { logAuditEvent(UserGroupInformation.getCurrentUGI(), Server.getRemotePeerID(), "append", src, null, null); } return lb; }
From source file:org.jspresso.framework.model.component.basic.AbstractComponentInvocationHandler.java
@SuppressWarnings("unchecked") protected void removeFromProperty(Object proxy, ICollectionPropertyDescriptor<?> propertyDescriptor, Object value) {/* ww w . j a v a 2 s.c o m*/ String propertyName = propertyDescriptor.getName(); // The following optimization breaks bidirectional N-N relationship persistence // if (!isInitialized(straightGetProperty(proxy, propertyName))) { // return; // } Collection<Object> collectionProperty = (Collection<Object>) straightGetProperty(proxy, propertyName); try { if (propertyProcessorsEnabled) { propertyDescriptor.preprocessRemover(proxy, collectionProperty, value); } if (collectionProperty.contains(value)) { IRelationshipEndPropertyDescriptor reversePropertyDescriptor = propertyDescriptor .getReverseRelationEnd(); if (reversePropertyDescriptor != null) { if (reversePropertyDescriptor instanceof IReferencePropertyDescriptor<?>) { accessorFactory .createPropertyAccessor(reversePropertyDescriptor.getName(), propertyDescriptor .getReferencedDescriptor().getElementDescriptor().getComponentContract()) .setValue(value, null); } else if (reversePropertyDescriptor instanceof ICollectionPropertyDescriptor<?>) { ICollectionAccessor collectionAccessor = accessorFactory.createCollectionPropertyAccessor( reversePropertyDescriptor.getName(), propertyDescriptor.getReferencedDescriptor().getElementDescriptor() .getComponentContract(), ((ICollectionPropertyDescriptor<?>) reversePropertyDescriptor) .getCollectionDescriptor().getElementDescriptor().getComponentContract()); if (collectionAccessor instanceof IModelDescriptorAware) { ((IModelDescriptorAware) collectionAccessor) .setModelDescriptor(reversePropertyDescriptor); } collectionAccessor.removeFromValue(value, proxy); } } Collection<?> oldCollectionSnapshot = CollectionHelper .cloneCollection((Collection<?>) collectionProperty); if (collectionProperty.remove(value)) { if (EntityHelper.isInlineComponentReference( propertyDescriptor.getReferencedDescriptor().getElementDescriptor())) { if (value != null) { ((IComponent) value).setOwningComponent(null, null); } } doFirePropertyChange(proxy, propertyName, oldCollectionSnapshot, collectionProperty); if (propertyProcessorsEnabled) { propertyDescriptor.postprocessRemover(proxy, collectionProperty, value); } if (proxy instanceof IEntity && value instanceof IEntity) { entityDetached((IEntity) proxy, (IEntity) value, propertyDescriptor); } } } } catch (RuntimeException ex) { rollbackProperty(proxy, propertyDescriptor, collectionProperty); throw ex; } catch (InvocationTargetException ex) { rollbackProperty(proxy, propertyDescriptor, collectionProperty); if (ex.getCause() instanceof RuntimeException) { throw (RuntimeException) ex.getCause(); } throw new ComponentException(ex.getCause()); } catch (IllegalAccessException | NoSuchMethodException ex) { throw new ComponentException(ex); } }
From source file:edu.amc.sakai.user.JLDAPDirectoryProvider.java
/** * Similar to iterating over <code>users</code> passing * each element to {@link #getUser(UserEdit)}, removing the * {@link org.sakaiproject.user.api.UserEdit} if that method * returns <code>false</code>. * /*from www . ja v a 2 s . com*/ * <p>Adds search retry capability if any one lookup fails * with a directory error. Empties <code>users</code> and * returns if a retry exits exceptionally * <p> */ public void getUsers(Collection<UserEdit> users) { if (M_log.isDebugEnabled()) { M_log.debug("getUsers(): [Collection size = " + users.size() + "]"); } LDAPConnection conn = null; boolean abortiveSearch = false; int maxQuerySize = getMaxObjectsToQueryFor(); UserEdit userEdit = null; HashMap<String, UserEdit> usersToSearchInLDAP = new HashMap<String, UserEdit>(); List<UserEdit> usersToRemove = new ArrayList<UserEdit>(); try { int cnt = 0; for (Iterator<UserEdit> userEdits = users.iterator(); userEdits.hasNext();) { userEdit = (UserEdit) userEdits.next(); String eid = userEdit.getEid(); if (!(isSearchableEid(eid))) { userEdits.remove(); //proceed ahead with this (perhaps the final) iteration //usersToSearchInLDAP needs to be processed unless empty } else { usersToSearchInLDAP.put(eid, userEdit); cnt++; } // We need to make sure this query isn't larger than maxQuerySize if ((!userEdits.hasNext() || cnt == maxQuerySize) && !usersToSearchInLDAP.isEmpty()) { if (conn == null) { conn = ldapConnectionManager.getConnection(); } String filter = ldapAttributeMapper.getManyUsersInOneSearch(usersToSearchInLDAP.keySet()); List<LdapUserData> ldapUsers = searchDirectory(filter, null, null, null, null, maxQuerySize); for (LdapUserData ldapUserData : ldapUsers) { String ldapEid = ldapUserData.getEid(); if (StringUtils.isEmpty(ldapEid)) { continue; } ldapEid = ldapEid.toLowerCase(); UserEdit ue = usersToSearchInLDAP.get(ldapEid); mapUserDataOntoUserEdit(ldapUserData, ue); usersToSearchInLDAP.remove(ldapEid); } // see if there are any users that we could not find in the LDAP query for (Map.Entry<String, UserEdit> entry : usersToSearchInLDAP.entrySet()) { usersToRemove.add(entry.getValue()); } // clear the HashMap and reset the counter usersToSearchInLDAP.clear(); cnt = 0; } } // Finally clean up the original collection and remove and users we could not find for (UserEdit userRemove : usersToRemove) { if (M_log.isDebugEnabled()) { M_log.debug("JLDAP getUsers could not find user: " + userRemove.getEid()); } users.remove(userRemove); } } catch (LDAPException e) { abortiveSearch = true; throw new RuntimeException("getUsers(): LDAPException during search [eid = " + (userEdit == null ? null : userEdit.getEid()) + "][result code = " + e.resultCodeToString() + "][error message = " + e.getLDAPErrorMessage() + "]", e); } catch (Exception e) { abortiveSearch = true; throw new RuntimeException("getUsers(): RuntimeException during search eid = " + (userEdit == null ? null : userEdit.getEid()) + "]", e); } finally { if (conn != null) { if (M_log.isDebugEnabled()) { M_log.debug("getUsers(): returning connection to connection manager"); } ldapConnectionManager.returnConnection(conn); } // no sense in returning a partially complete search result if (abortiveSearch) { if (M_log.isDebugEnabled()) { M_log.debug("getUsers(): abortive search, clearing received users collection"); } users.clear(); } } }
From source file:org.apache.jetspeed.portlets.site.PortalSiteManager.java
protected void removeMetadata(JetspeedDocumentMetaData metaData, Node node) { Collection cfields = node.getMetadata().getFields(metaData.getName()); Collection allFields = node.getMetadata().getFields(); if (cfields == null || cfields.size() == 0) { return;/*from w ww .j a va 2s . com*/ } boolean found = false; Iterator fields = cfields.iterator(); while (fields.hasNext()) { LocalizedField field = (LocalizedField) fields.next(); if (areFieldsSame(field.getName(), metaData.getName()) && areFieldsSame(field.getLocale().toString(), metaData.getLanguage())) { cfields.remove(field); if (allFields.remove(field)) { node.getMetadata().setFields(allFields); } found = true; break; } } }
From source file:com.google.gwt.emultest.java.util.TreeMapTest.java
/** * Test method for 'java.util.Map.values()'. * * @see java.util.Map#values()/*w w w .j av a 2 s. c o m*/ */ public void testValues() { K[] keys = getSortedKeys(); V[] values = getSortedValues(); NavigableMap<K, V> map = createNavigableMap(); map.put(keys[0], values[0]); Collection<V> mapValues = map.values(); assertNotNull(mapValues); assertEquals(1, mapValues.size()); Iterator<V> valueIter = mapValues.iterator(); assertEquals(values[0], valueIter.next()); _assertEquals(mapValues, map.values()); mapValues.clear(); _assertEmpty(map); for (int i = 0; i < keys.length; i++) { map.put(keys[i], values[i]); } mapValues.iterator(); assertEquals(map.size(), mapValues.size()); for (V value : values) { assertTrue(mapValues.contains(value)); } assertEquals(values.length, mapValues.size()); int size = 0; for (Iterator iter = mapValues.iterator(); iter.hasNext(); iter.next()) { size++; } assertEquals(values.length, size); mapValues = map.descendingMap().values(); mapValues.iterator(); assertEquals(map.size(), mapValues.size()); for (V value : values) { assertTrue(mapValues.contains(value)); } assertEquals(values.length, mapValues.size()); size = 0; for (Iterator iter = mapValues.iterator(); iter.hasNext(); iter.next()) { size++; } assertEquals(values.length, size); mapValues = map.values(); mapValues.remove(values[0]); assertTrue(!map.containsValue(values[0])); assertEquals(values.length - 1, mapValues.size()); size = 0; for (Iterator iter = mapValues.iterator(); iter.hasNext(); iter.next()) { size++; } assertEquals(values.length - 1, size); }
From source file:org.diorite.impl.inventory.recipe.craft.ShapelessCraftingRecipeImpl.java
@Override public CraftingRecipeCheckResult isMatching(final GridInventory inventory) { final Player player = (inventory.getHolder() instanceof Player) ? (Player) inventory.getHolder() : null; final Short2ObjectMap<ItemStack> onCraft = new Short2ObjectOpenHashMap<>(2, .5F); final int maxInvRow = inventory.getRows(), maxInvCol = inventory.getColumns(); final LinkedList<CraftingRecipeItem> ingredients = new LinkedList<>(this.getIngredients()); final Collection<CraftingRepeatableRecipeItem> repeatableIngredients = new LinkedList<>( this.getRepeatableIngredients()); final Collection<BiConsumer<Player, CraftingGrid>> reps = new ArrayList<>(maxInvCol * maxInvRow); final CraftingGrid items = new CraftingGridImpl(maxInvRow, maxInvCol); int col = -1, row = 0; ItemStack result;//from w ww .ja va 2 s . c om if (this.repeatableIngredients.isEmpty()) { for (short i = 1, size = (short) inventory.size(); i < size; i++) { if (++col >= maxInvCol) { col = 0; if (++row > maxInvRow) { throw new IllegalStateException("Inventory is larger than excepted."); } } final ItemStack item = inventory.getItem(i); if (item == null) { continue; } boolean matching = false; //noinspection Duplicates for (final Iterator<CraftingRecipeItem> iterator = ingredients.iterator(); iterator.hasNext();) { final CraftingRecipeItem ingredient = iterator.next(); final ItemStack valid = ingredient.isValid(player, item); if (valid != null) { final short icpy = i; reps.add((p, c) -> { final ItemStack repl = ingredient.getReplacement(p, c); if (repl != null) { onCraft.put(icpy, repl); } }); items.setItem(row, col, valid); iterator.remove(); matching = true; break; } } if (!matching) { return null; } } result = (this.resultFunc == null) ? this.result : this.resultFunc.apply(player, items.clone()); } else { final Map<CraftingRepeatableRecipeItem, List<ItemStack>> repeatableItems = new IdentityHashMap<>( this.repeatableIngredients.size()); for (short i = 1, size = (short) inventory.size(); i < size; i++) { if (++col >= maxInvCol) { col = 0; if (++row > maxInvRow) { throw new IllegalStateException("Inventory is larger than excepted."); } } final ItemStack item = inventory.getItem(i); if (item == null) { continue; } boolean matching = false; if (!ingredients.isEmpty()) { //noinspection Duplicates for (final Iterator<CraftingRecipeItem> iterator = ingredients.iterator(); iterator .hasNext();) { final CraftingRecipeItem ingredient = iterator.next(); final ItemStack valid = ingredient.isValid(player, item); if (valid != null) { final short icpy = i; reps.add((p, c) -> { final ItemStack repl = ingredient.getReplacement(p, c); if (repl != null) { onCraft.put(icpy, repl); } }); items.setItem(row, col, valid); iterator.remove(); matching = true; break; } } } if (!matching) { for (final CraftingRepeatableRecipeItem ingredient : this.repeatableIngredients) { final ItemStack valid = ingredient.isValid(player, item); if (valid != null) { final short icpy = i; reps.add((p, c) -> { final ItemStack repl = ingredient.getReplacement(p, c); if (repl != null) { onCraft.put(icpy, repl); } }); List<ItemStack> repItems = repeatableItems.get(ingredient); if (repItems == null) { repeatableIngredients.remove(ingredient); repItems = new ArrayList<>(10); repeatableItems.put(ingredient, repItems); } repItems.add(valid); items.setItem(row, col, valid); matching = true; break; } } } if (!matching) { return null; } } if (!repeatableIngredients.isEmpty()) { return null; } result = (this.resultFunc == null) ? this.result : this.resultFunc.apply(player, items.clone()); for (final Entry<CraftingRepeatableRecipeItem, List<ItemStack>> entry : repeatableItems.entrySet()) { result = entry.getKey().transform(result, entry.getValue()); result = entry.getKey().transform(result, items); } } reps.forEach(c -> c.accept(player, items)); return ingredients.isEmpty() ? new CraftingRecipeCheckResultImpl(this, result, items, onCraft) : null; }
From source file:org.ms123.common.data.JdoLayerImpl.java
public void populate(SessionContext sessionContext, Map from, Object to, Map hintsMap) { PersistenceManager pm = sessionContext.getPM(); if (hintsMap == null) { hintsMap = new HashMap(); }/* www .jav a2 s.c o m*/ Map<String, String> expressions = (Map) hintsMap.get("__expressions"); if (expressions == null) expressions = new HashMap(); BeanMap beanMap = new BeanMap(to); String entityName = m_inflector.getEntityName(to.getClass().getSimpleName()); debug("populate.from:" + from + ",to:" + to + ",BeanMap:" + beanMap + "/hintsMap:" + hintsMap + "/entityName:" + entityName); if (from == null) { return; } Map permittedFields = sessionContext.getPermittedFields(entityName, "write"); Iterator<String> it = from.keySet().iterator(); while (it.hasNext()) { String key = it.next(); Object oldValue = beanMap.get(key); boolean permitted = m_permissionService.hasAdminRole() || "team".equals(entityName) || sessionContext.isFieldPermitted(key, entityName, "write"); if (!key.startsWith("_") && !permitted) { debug("---->populate:field(" + key + ") no write permission"); continue; } else { debug("++++>populate:field(" + key + ") write permitted"); } String datatype = null; String edittype = null; if (!key.startsWith("_")) { Map config = (Map) permittedFields.get(key); if (config != null) { datatype = (String) config.get("datatype"); edittype = (String) config.get("edittype"); } } if (key.equals(STATE_FIELD) && !m_permissionService.hasAdminRole()) { continue; } if ("auto".equals(edittype)) continue; String mode = null; Map hm = (Map) hintsMap.get(key); 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 clazz = beanMap.getType(key); debug("\ttype:" + clazz + "(" + key + "=" + from.get(key) + ")"); if ("_ignore_".equals(from.get(key))) { continue; } if (clazz == null) { debug("\t--- Warning property not found:" + key); } else if (clazz.equals(java.util.Date.class)) { String value = Utils.getString(from.get(key), beanMap.get(key), mode); debug("\tDate found:" + key + "=>" + 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); beanMap.put(key, date); } else if (clazz.equals(java.util.Map.class)) { info("!!!!!!!!!!!!!!!!!!!Map not implemented"); } else if (clazz.equals(java.util.List.class) || clazz.equals(java.util.Set.class)) { boolean isList = clazz.equals(java.util.List.class); boolean isSimple = false; if (datatype != null && datatype.startsWith("list_")) { isSimple = true; } try { Class type = TypeUtils.getTypeForField(to, key); debug("type:" + type + " fill with: " + from.get(key) + ",list:" + beanMap.get(key) + "/mode:" + mode); Collection valList = isList ? new ArrayList() : new HashSet(); Object fromVal = from.get(key); if (fromVal instanceof String && ((String) fromVal).length() > 0) { info("FromVal is StringSchrott, ignore"); continue; } if (from.get(key) instanceof Collection) { valList = (Collection) from.get(key); } if (valList == null) { valList = isList ? new ArrayList() : new HashSet(); } Collection toList = (Collection) PropertyUtils.getProperty(to, key); debug("toList:" + toList); debug("valList:" + valList); if (toList == null) { toList = isList ? new ArrayList() : new HashSet(); PropertyUtils.setProperty(to, key, toList); } if ("replace".equals(mode)) { boolean isEqual = false; if (isSimple) { isEqual = Utils.isCollectionEqual(toList, valList); if (!isEqual) { toList.clear(); } debug("\tisEqual:" + isEqual); } else { List deleteList = new ArrayList(); String namespace = sessionContext.getStoreDesc().getNamespace(); for (Object o : toList) { if (type.getName().endsWith(".Team")) { int status = m_teamService.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) { toList.remove(o); } } debug("populate.replace.toList:" + toList + "/" + type.getName()); if (isSimple) { if (!isEqual) { for (Object o : valList) { toList.add(o); } } } else { for (Object o : valList) { Map valMap = (Map) o; Object n = type.newInstance(); if (type.getName().endsWith(".Team")) { valMap.remove("id"); Object desc = valMap.get("description"); Object name = valMap.get("name"); Object dis = valMap.get("disabled"); String teamid = (String) valMap.get("teamid"); Object ti = Utils.getTeamintern(sessionContext, teamid); if (desc == null) { valMap.put("description", PropertyUtils.getProperty(ti, "description")); } if (name == null) { valMap.put("name", PropertyUtils.getProperty(ti, "name")); } if (dis == null) { valMap.put("disabled", false); } pm.makePersistent(n); populate(sessionContext, valMap, n, null); PropertyUtils.setProperty(n, "teamintern", ti); } else { pm.makePersistent(n); populate(sessionContext, valMap, n, null); } debug("populated.add:" + new HashMap(new BeanMap(n))); toList.add(n); } } } else if ("remove".equals(mode)) { if (isSimple) { for (Object o : valList) { if (toList.contains(o)) { toList.remove(o); } } } else { for (Object ol : valList) { Map map = (Map) ol; Object o = Utils.listContainsId(toList, map, "teamid"); if (o != null) { toList.remove(o); pm.deletePersistent(o); } } } } else if ("add".equals(mode)) { if (isSimple) { for (Object o : valList) { toList.add(o); } } else { for (Object ol : valList) { Map map = (Map) ol; Object o = Utils.listContainsId(toList, map, "teamid"); if (o != null) { populate(sessionContext, map, o, null); } else { o = type.newInstance(); if (type.getName().endsWith(".Team")) { Object desc = map.get("description"); Object name = map.get("name"); Object dis = map.get("disabled"); String teamid = (String) map.get("teamid"); Object ti = Utils.getTeamintern(sessionContext, teamid); if (desc == null) { map.put("description", PropertyUtils.getProperty(ti, "description")); } if (name == null) { map.put("name", PropertyUtils.getProperty(ti, "name")); } if (dis == null) { map.put("disabled", false); } pm.makePersistent(o); populate(sessionContext, map, o, null); PropertyUtils.setProperty(o, "teamintern", ti); } else { pm.makePersistent(o); populate(sessionContext, map, o, null); } toList.add(o); } } } } else if ("assign".equals(mode)) { if (!isSimple) { for (Object ol : valList) { Map map = (Map) ol; Object o = Utils.listContainsId(toList, map); if (o != null) { debug("id:" + map + " already assigned"); } else { Object id = map.get("id"); Boolean assign = Utils.getBoolean(map.get("assign")); Object obj = pm.getObjectById(type, id); if (assign) { toList.add(obj); } else { toList.remove(obj); } } } } } } catch (Exception e) { e.printStackTrace(); debug("populate.list.failed:" + key + "=>" + from.get(key) + ";" + e); } } else if (clazz.equals(java.lang.Boolean.class)) { try { beanMap.put(key, ConvertUtils.convert(from.get(key), Boolean.class)); } catch (Exception e) { debug("populate.boolean.failed:" + key + "=>" + from.get(key) + ";" + e); } } else if (clazz.equals(java.lang.Double.class)) { String value = Utils.getString(from.get(key), beanMap.get(key), mode); try { beanMap.put(key, Double.valueOf(value)); } catch (Exception e) { debug("populate.double.failed:" + key + "=>" + value + ";" + e); } } else if (clazz.equals(java.lang.Long.class)) { try { beanMap.put(key, ConvertUtils.convert(from.get(key), Long.class)); } catch (Exception e) { debug("populate.long.failed:" + key + "=>" + from.get(key) + ";" + e); } } else if (clazz.equals(java.lang.Integer.class)) { debug("Integer:" + ConvertUtils.convert(from.get(key), Integer.class)); try { beanMap.put(key, ConvertUtils.convert(from.get(key), Integer.class)); } catch (Exception e) { debug("populate.integer.failed:" + key + "=>" + from.get(key) + ";" + e); } } else if ("binary".equals(datatype) || clazz.equals(byte[].class)) { InputStream is = null; InputStream is2 = null; try { if (from.get(key) instanceof FileItem) { FileItem fi = (FileItem) from.get(key); String name = fi.getName(); byte[] bytes = IOUtils.toByteArray(fi.getInputStream()); if (bytes != null) { debug("bytes:" + bytes.length); } beanMap.put(key, bytes); is = fi.getInputStream(); is2 = fi.getInputStream(); } else if (from.get(key) instanceof Map) { Map map = (Map) from.get(key); 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(); beanMap.put(key, bytes); is = new FileInputStream(new File(storeLocation)); } else if (from.get(key) instanceof String) { String value = (String) from.get(key); if ("ignore".equals(value)) { debug("ignore:"); return; } if (value.startsWith("data:")) { int ind = value.indexOf(";base64,"); byte b[] = Base64.decode(value.substring(ind + 8)); beanMap.put(key, b); is = new ByteArrayInputStream(b); is2 = new ByteArrayInputStream(b); } else { byte b[] = value.getBytes(); beanMap.put(key, b); is = new ByteArrayInputStream(b); is2 = new ByteArrayInputStream(b); } } else { debug("populate.byte[].no a FileItem:" + key + "=>" + from.get(key)); continue; } Tika tika = new Tika(); TikaInputStream stream = TikaInputStream.get(is); TikaInputStream stream2 = TikaInputStream.get(is2); String text = tika.parseToString(is); debug("Text:" + text); try { beanMap.put("text", text); } catch (Exception e) { beanMap.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()); beanMap.put("type", mime.toString()); from.put("type", mime.toString()); } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); debug("populate.byte[].failed:" + key + "=>" + from.get(key) + ";" + e); } finally { try { is.close(); is2.close(); } catch (Exception e) { } } } else { boolean ok = false; try { Class type = TypeUtils.getTypeForField(to, key); if (type != null) { Object o = type.newInstance(); boolean hasAnn = type.isAnnotationPresent(PersistenceCapable.class); debug("hasAnnotation:" + hasAnn); if (o instanceof javax.jdo.spi.PersistenceCapable || hasAnn) { Object id = null; try { Object _id = from.get(key); if (_id != null) { if (_id instanceof Map) { id = ((Map) _id).get("id"); } else { String s = String.valueOf(_id); if (s.indexOf("/") >= 0) { _id = Utils.extractId(s); } Class idClass = PropertyUtils.getPropertyType(o, "id"); id = (idClass.equals(Long.class)) ? Long.valueOf(_id + "") : _id; } } } catch (Exception e) { } if (id != null && !"".equals(id) && !"null".equals(id)) { debug("\tId2:" + id); Object relatedObject = pm.getObjectById(type, id); List<Collection> candidates = TypeUtils.getCandidateLists(relatedObject, to, null); if (candidates.size() == 1) { Collection l = candidates.get(0); debug("list.contains:" + l.contains(to)); if (!l.contains(to)) { l.add(to); } } beanMap.put(key, relatedObject); } else { Object relatedObject = beanMap.get(key); debug("\trelatedObject:" + relatedObject); if (relatedObject != null) { List<Collection> candidates = TypeUtils.getCandidateLists(relatedObject, to, null); if (candidates.size() == 1) { Collection l = candidates.get(0); debug("list.contains:" + l.contains(to)); if (l.contains(to)) { l.remove(to); } } } beanMap.put(key, null); } ok = true; } } } catch (Exception e) { e.printStackTrace(); } if (!ok) { String value = Utils.getString(from.get(key), beanMap.get(key), mode); // debug("populate:" + key + "=>" + value); // debug("String:" + ConvertUtils.convert(from.get(key), String.class)); try { beanMap.put(key, value); } catch (Exception e) { debug("populate.failed:" + key + "=>" + value + ";" + e); } } } String expression = expressions.get(key); if (!isEmpty(expression)) { beanMap.put(key, oldValue); Map scriptCache = (Map) sessionContext.getProperty("scriptCache"); if (scriptCache == null) { scriptCache = new HashMap(); sessionContext.setProperty("scriptCache", scriptCache); } Object result = Utils.eval(expression, beanMap, scriptCache); try { if ("string".equals(datatype) && !(result instanceof String)) { beanMap.put(key, ConvertUtils.convert(result, String.class)); } else { beanMap.put(key, result); } } catch (Exception e) { info("Cannot set value for(" + key + "):" + result + "/" + e.getMessage()); } } } }
From source file:org.regenstrief.util.Util.java
public final static boolean remove(final Collection<?> c, final Object elem) { return (c != null) && c.remove(elem); }
From source file:org.biomart.configurator.controller.MartController.java
public void updateDatasetFromSource(Dataset ds) throws SQLException, DataModelException { Mart mart = ds.getParentMart();/* ww w. ja va 2 s . c o m*/ List<SourceSchema> sss = mart.getIncludedSchemas(); // assuming that only one for now if (McUtils.isCollectionEmpty(sss)) return; SourceSchema ss = sss.get(0); final DatabaseMetaData dmd = ss.getConnection().getMetaData(); final String catalog = ss.getConnection().getCatalog(); // List of objects storing orphan key column and its table name List<ForeignKey> orphanFKList = new ArrayList<ForeignKey>(); StringBuffer orphanSearch = new StringBuffer(); boolean orphanBool = false; /* * try { orphanBool = findOrphanFKFromDB(orphanFKList, orphanSearch, mart); if (orphanBool) { Frame frame = new * Frame(); Object[] options = { "Proceed", "Abort Synchronization" }; int n = JOptionPane .showOptionDialog( * frame, * "Some columns in relations no longer exist in source DB. This may be caused by renaming/dropping tables/columns in source DB.\n" * + * "When choose 'Proceed', you will be prompted to save this information for later use. Do you want to proceed?" * +"\n", "Schema Update Warning", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, // do // not // * use a // custom // Icon options, // the titles of buttons options[1]); // default button title if (n == * JOptionPane.NO_OPTION) { return; } else{ new SaveOrphanKeyDialog("Orphan Relation", orphanSearch.toString()); * } } } catch (Exception e) { e.printStackTrace(); } // Now that user decides to sync GUI model to DB schema, * remove orphan foreign key clearOrphanForeignKey(orphanFKList); */ // Create a list of existing tables. During this method, we remove // from this list all tables that still exist in the database. At // the end of the method, the list contains only those tables // which no longer exist, so they will be dropped. final Collection<Table> tablesToBeDropped = new HashSet<Table>(ss.getTables()); // Load tables and views from database, then loop over them. ResultSet dbTables; String sName = ss.getJdbcLinkObject().getJdbcType().useSchema() ? ss.getJdbcLinkObject().getSchemaName() : catalog; dbTables = dmd.getTables(catalog, sName, "%", new String[] { "TABLE", "VIEW", "ALIAS", "SYNONYM" }); // Do the loop. final Collection<Table> tablesToBeKept = new HashSet<Table>(); while (dbTables.next()) { // Check schema and catalog. final String catalogName = dbTables.getString("TABLE_CAT"); final String schemaName = dbTables.getString("TABLE_SCHEM"); String schemaPrefix = schemaName; // What is the table called? final String dbTableName = dbTables.getString("TABLE_NAME"); Log.debug("Processing table " + catalogName + "." + dbTableName); // Look to see if we already have a table by this name defined. // If we do, reuse it. If not, create a new table. Table dbTable = ss.getTableByName(dbTableName); if (dbTable == null) try { dbTable = new SourceTable(ss, dbTableName); dbTable.setVisibleModified(true); ss.addTable(dbTable); } catch (final Throwable t) { throw new BioMartError(t); } // Add schema prefix to list. if (schemaPrefix != null) dbTable.addInPartitions(schemaPrefix); // Table exists, so remove it from our list of tables to be // dropped at the end of the method. tablesToBeDropped.remove(dbTable); tablesToBeKept.add(dbTable); } dbTables.close(); // Loop over all columns. for (final Iterator<Table> i = tablesToBeKept.iterator(); i.hasNext();) { final Table dbTable = (Table) i.next(); final String dbTableName = dbTable.getName(); // Make a list of all the columns in the table. Any columns // remaining in this list by the end of the loop will be // dropped. final Collection<Column> colsToBeDropped = new HashSet<Column>(dbTable.getColumnList()); // Clear out the existing schema partition info on all cols. for (final Iterator<Column> j = dbTable.getColumnList().iterator(); j.hasNext();) ((Column) j.next()).cleanInPartitions(); // Load the table columns from the database, then loop over // them. Log.debug("Loading table column list for " + dbTableName); ResultSet dbTblCols; dbTblCols = dmd.getColumns(catalog, sName, dbTableName, "%"); // FIXME: When using Oracle, if the table is a synonym then the // above call returns no results. while (dbTblCols.next()) { // Check schema and catalog. final String catalogName = dbTblCols.getString("TABLE_CAT"); final String schemaName = dbTblCols.getString("TABLE_SCHEM"); String schemaPrefix = null; // No prefix if partitions are empty; /* * if (!this.getPartitions().isEmpty()) { if ("".equals(dmd.getSchemaTerm())) // Use catalog name to get * prefix. schemaPrefix = (String) this.getPartitions().get( catalogName); else // Use schema name to * get prefix. schemaPrefix = (String) this.getPartitions().get( schemaName); // Don't want to include * if prefix is still null. if (schemaPrefix == null) continue; } */ // What is the column called, and is it nullable? final String dbTblColName = dbTblCols.getString("COLUMN_NAME"); Log.debug("Processing column " + dbTblColName); // Look to see if the column already exists on this table. // If it does, reuse it. Else, create it. Column dbTblCol = (Column) dbTable.getColumnByName(dbTblColName); if (dbTblCol == null) try { dbTblCol = new SourceColumn((SourceTable) dbTable, dbTblColName); dbTblCol.setVisibleModified(true); dbTable.addColumn(dbTblCol); } catch (final Throwable t) { throw new BioMartError(t); } // Column exists, so remove it from our list of columns to // be dropped at the end of the loop. colsToBeDropped.remove(dbTblCol); if (schemaPrefix != null) dbTblCol.addInPartitions(schemaPrefix); } dbTblCols.close(); // Drop all columns that are left in the list, as they no longer // exist in the database. for (final Iterator<Column> j = colsToBeDropped.iterator(); j.hasNext();) { final Column column = (Column) j.next(); Log.debug("Dropping redundant column " + column.getName()); dbTable.getColumnList().remove(column); } } // Remove from schema all tables not found in the database, using // the list we constructed above. for (final Iterator<Table> i = tablesToBeDropped.iterator(); i.hasNext();) { final Table existingTable = (Table) i.next(); Log.debug("Dropping redundant table " + existingTable); final String tableName = existingTable.getName(); // By clearing its keys we will also clear its relations. for (final Iterator<Key> j = existingTable.getKeys().iterator(); j.hasNext();) { j.next().removeAllRelations(); } existingTable.setPrimaryKey(null); existingTable.getForeignKeys().clear(); ss.removeTableByName(tableName); } // Get and create primary keys. // Work out a list of all foreign keys currently existing. // Any remaining in this list later will be dropped. final Collection<ForeignKey> fksToBeDropped = new HashSet<ForeignKey>(); for (final Iterator<Table> i = ss.getTables().iterator(); i.hasNext();) { final Table t = (Table) i.next(); fksToBeDropped.addAll(t.getForeignKeys()); // Obtain the primary key from the database. Even in databases // without referential integrity, the primary key is still // defined and can be obtained from the metadata. Log.debug("Loading table primary keys"); String searchCatalog = catalog; String searchSchema = sName; /* * if (!t.getSchemaPartitions().isEmpty()) { // Locate partition with first prefix. final String prefix = * (String) t.getSchemaPartitions() .iterator().next(); String schemaName = (String) new InverseMap(this * .getPartitions()).get(prefix); if (schemaName == null) // Should never happen. throw new BioMartError(); * if ("".equals(dmd.getSchemaTerm())) searchCatalog = schemaName; searchSchema = schemaName; } */ final ResultSet dbTblPKCols = dmd.getPrimaryKeys(searchCatalog, searchSchema, t.getName()); // Load the primary key columns into a map keyed by column // position. // In other words, the first column in the key has a map key of // 1, and so on. We do this because we can't guarantee we'll // read the key columns from the database in the correct order. // We keep the map sorted, so that when we iterate over it later // we get back the columns in the correct order. final Map<Short, Column> pkCols = new TreeMap<Short, Column>(); while (dbTblPKCols.next()) { final String pkColName = dbTblPKCols.getString("COLUMN_NAME"); final Short pkColPosition = new Short(dbTblPKCols.getShort("KEY_SEQ")); pkCols.put(pkColPosition, t.getColumnByName(pkColName)); } dbTblPKCols.close(); // Did DMD find a PK? If not, which is really unusual but // potentially may happen, attempt to find one by looking for a // single column with the same name as the table or with '_id' // appended. // Only do this if we are using key-guessing. if (pkCols.isEmpty() && ss.getJdbcLinkObject().isKeyGuessing()) { Log.debug("Found no primary key, so attempting to guess one"); // Plain version first. Column candidateCol = (Column) t.getColumnByName(t.getName()); // Try with '_id' appended if plain version turned up // nothing. if (candidateCol == null) candidateCol = (Column) t.getColumnByName(t.getName() + Resources.get("primaryKeySuffix")); // Found something? Add it to the primary key columns map, // with a dummy key of 1. (Use Short for the key because // that // is what DMD would have used had it found anything // itself). if (candidateCol != null) pkCols.put(Short.valueOf("1"), candidateCol); } // Obtain the existing primary key on the table, if the table // previously existed and even had one in the first place. final PrimaryKey existingPK = t.getPrimaryKey(); // Did we find a PK on the database copy of the table? if (!pkCols.isEmpty()) { // Yes, we found a PK on the database copy of the table. So, // create a new key based around the columns we identified. PrimaryKey candidatePK; try { candidatePK = new PrimaryKey(new ArrayList<Column>(pkCols.values())); } catch (final Throwable th) { throw new BioMartError(th); } // If the existing table has no PK, or has a PK which // matches and is not incorrect, or has a PK which does not // match // and is not handmade, replace that PK with the one we // found. // This way we preserve any existing handmade PKs, and don't // override any marked as incorrect. try { if (existingPK == null) t.setPrimaryKey(candidatePK); else if (existingPK.equals(candidatePK) && existingPK.getStatus().equals(ComponentStatus.HANDMADE)) existingPK.setStatus(ComponentStatus.INFERRED); else if (!existingPK.equals(candidatePK) && !existingPK.getStatus().equals(ComponentStatus.HANDMADE)) t.setPrimaryKey(candidatePK); } catch (final Throwable th) { throw new BioMartError(th); } } else // No, we did not find a PK on the database copy of the // table, so that table should not have a PK at all. So if the // existing table has a PK which is not handmade, remove it. if (existingPK != null && !existingPK.getStatus().equals(ComponentStatus.HANDMADE)) try { t.setPrimaryKey(null); } catch (final Throwable th) { throw new BioMartError(th); } } // Are we key-guessing? Key guess the foreign keys, passing in a // reference to the list of existing foreign keys. After this call // has completed, the list will contain all those foreign keys which // no longer exist, and can safely be dropped. if (ss.getJdbcLinkObject().isKeyGuessing()) this.synchroniseKeysUsingKeyGuessing(ss, fksToBeDropped); // Otherwise, use DMD to do the same, also passing in the list of // existing foreign keys to be updated as the call progresses. Also // pass in the DMD details so it doesn't have to work them out for // itself. else this.synchroniseKeysUsingDMD(ss, fksToBeDropped, dmd, sName, catalog); // Drop any foreign keys that are left over (but not handmade ones). for (final Iterator<ForeignKey> i = fksToBeDropped.iterator(); i.hasNext();) { final Key k = (Key) i.next(); if (k.getStatus().equals(ComponentStatus.HANDMADE)) continue; Log.debug("Dropping redundant foreign key " + k); for (final Iterator<Relation> r = k.getRelations().iterator(); r.hasNext();) { final Relation rel = (Relation) r.next(); rel.getFirstKey().getRelations().remove(rel); rel.getSecondKey().getRelations().remove(rel); } k.getTable().getForeignKeys().remove(k); } // rebuild mart this.rebuildMartFromSource(mart); }
From source file:it.cnr.icar.eric.client.ui.thin.RegistryObjectCollectionBean.java
@SuppressWarnings("unchecked") public void handleRegistryPackageMembers(@SuppressWarnings("rawtypes") Collection robs) throws JAXRException { List<?> tempList = new ArrayList<Object>(); tempList.addAll(robs);/*from ww w. j a va 2 s . c o m*/ Iterator<?> robItr = tempList.iterator(); while (robItr.hasNext()) { RegistryObjectBean rob = (RegistryObjectBean) robItr.next(); RegistryObject ro = rob.getRegistryObject(); if (ro instanceof RegistryPackageImpl) { RegistryPackageImpl rp = (RegistryPackageImpl) ro; // Handle removal of Members Iterator<RegistryObject> itr = rp.getRegistryObjects().iterator(); while (itr.hasNext()) { RegistryObject mro = itr.next(); RegistryObjectBean mrob = registryObjectLookup.get(mro.getKey().getId()); if (mrob != null && mrob.isRemoveRoFromRegistryPackage()) { rp.removeRegistryObject(mro); mrob.setRemoveRoFromRegistryPackage(false); mrob.setAddRoToRegistryPackage(false); mrob.setRelatedSelected(false); robs.remove(rob); } } //Handle addition of Members List<RegistryObjectBean> relatedList = getRelatedRegistryObjectBeans(); if (relatedList != null) { Iterator<RegistryObjectBean> addItr = relatedList.iterator(); while (addItr.hasNext()) { RegistryObjectBean mrob = addItr.next(); if (mrob.isAddRoToRegistryPackage()) { rp.addRegistryObject(mrob.getRegistryObject()); mrob.setAddRoToRegistryPackage(false); } } } } } }