Example usage for org.apache.commons.collections CollectionUtils collect

List of usage examples for org.apache.commons.collections CollectionUtils collect

Introduction

In this page you can find the example usage for org.apache.commons.collections CollectionUtils collect.

Prototype

public static Collection collect(Iterator inputIterator, Transformer transformer) 

Source Link

Document

Transforms all elements from the inputIterator with the given transformer and adds them to the outputCollection.

Usage

From source file:org.sipfoundry.sipxconfig.api.PhoneBuilder.java

public void toApiObject(Object apiObject, Object myObject, Set properties) {
    super.toApiObject(apiObject, myObject, properties);
    Phone phone = (Phone) apiObject;/* w  w w  .ja v  a  2 s. c om*/
    org.sipfoundry.sipxconfig.phone.Phone otherPhone = (org.sipfoundry.sipxconfig.phone.Phone) myObject;
    if (properties.contains(MODEL_ID_PROP)) {
        phone.setModelId(otherPhone.getModelId());
    }
    if (properties.contains(GROUPS_PROP)) {
        Collection groupNames = CollectionUtils.collect(otherPhone.getGroups(), new NamedObject.ToName());
        phone.setGroups((String[]) groupNames.toArray(new String[groupNames.size()]));
    }
    if (properties.contains(LINES_PROP)) {
        List myLines = otherPhone.getLines();
        if (myLines.size() > 0) {
            Line[] apiLines = (Line[]) ApiBeanUtil.newArray(Line.class, myLines.size());
            for (int i = 0; i < apiLines.length; i++) {
                org.sipfoundry.sipxconfig.phone.Line myLine = (org.sipfoundry.sipxconfig.phone.Line) myLines
                        .get(i);
                LineInfo lineInfo = myLine.getLineInfo();
                apiLines[i].setUserId(lineInfo.getUserId());
                apiLines[i].setUri(myLine.getUri());
            }
            phone.setLines(apiLines);
        }
    }
    DeviceVersion ver = otherPhone.getDeviceVersion();
    if (ver != null && properties.contains(DEVICE_VER)) {
        phone.setDeviceVersion(ver.getName());
    }
}

From source file:org.sipfoundry.sipxconfig.api.PhoneServiceImpl.java

public void managePhone(ManagePhone managePhone) throws RemoteException {
    org.sipfoundry.sipxconfig.phone.Phone[] myPhones = phoneSearch(managePhone.getSearch());
    Collection ids = CollectionUtils.collect(Arrays.asList(myPhones), new BeanWithId.BeanToId());
    if (Boolean.TRUE.equals(managePhone.getGenerateProfiles())) {
        m_profileManager.generateProfiles(ids, false, null);
    } else if (Boolean.TRUE.equals(managePhone.getRestart())) {
        m_restartManager.restart(ids, null);
    } else {/*w w  w. ja  va 2 s  .co  m*/
        for (int i = 0; i < myPhones.length; i++) {

            if (Boolean.TRUE.equals(managePhone.getDeletePhone())) {
                m_context.deletePhone(myPhones[i]);
                continue; // all other edits wouldn't make sense
            }

            if (managePhone.getEdit() != null) {
                Phone apiPhone = new Phone();
                Set properties = ApiBeanUtil.getSpecifiedProperties(managePhone.getEdit());
                ApiBeanUtil.setProperties(apiPhone, managePhone.getEdit());
                m_builder.toMyObject(myPhones[i], apiPhone, properties);
                m_context.storePhone(myPhones[i]);
            }

            if (managePhone.getRemoveLineByUserId() != null) {
                String username = managePhone.getRemoveLineByUserId();
                org.sipfoundry.sipxconfig.phone.Line l = myPhones[i].findByUsername(username);
                if (l != null) {
                    m_context.deleteLine(l);
                }
            }

            if (managePhone.getRemoveLineByUri() != null) {
                String uri = managePhone.getRemoveLineByUri();
                org.sipfoundry.sipxconfig.phone.Line l = myPhones[i].findByUri(uri);
                if (l != null) {
                    m_context.deleteLine(l);
                }
            }

            if (managePhone.getAddLine() != null) {
                String userName = managePhone.getAddLine().getUserId();
                org.sipfoundry.sipxconfig.common.User u = m_coreContext.loadUserByUserName(userName);
                org.sipfoundry.sipxconfig.phone.Line l = myPhones[i].createLine();
                l.setUser(u);
                myPhones[i].addLine(l);
                m_context.storePhone(myPhones[i]);
            }

            if (managePhone.getAddExternalLine() != null) {
                AddExternalLine eline = managePhone.getAddExternalLine();
                org.sipfoundry.sipxconfig.phone.Line l = myPhones[i].createLine();
                myPhones[i].addLine(l);
                LineInfo einfo = new LineInfo();
                ApiBeanUtil.toMyObject(new SimpleBeanBuilder(), einfo, eline);
                l.setLineInfo(einfo);
                m_context.storePhone(myPhones[i]);
            }

            if (managePhone.getAddGroup() != null) {
                Group g = m_settingDao.getGroupCreateIfNotFound(GROUP_RESOURCE_ID, managePhone.getAddGroup());
                myPhones[i].addGroup(g);
                m_context.storePhone(myPhones[i]);
            }

            if (managePhone.getRemoveGroup() != null) {
                Group g = m_settingDao.getGroupByName(GROUP_RESOURCE_ID, managePhone.getRemoveGroup());
                if (g != null) {
                    DataCollectionUtil.removeByPrimaryKey(myPhones[i].getGroups(), g.getPrimaryKey());
                }
                m_context.storePhone(myPhones[i]);
            }
        }
    }
}

From source file:org.sipfoundry.sipxconfig.api.UserBuilder.java

public void toApiObject(Object apiObject, Object myObject, Set properties) {
    super.toApiObject(apiObject, myObject, properties);
    org.sipfoundry.sipxconfig.common.User my = (org.sipfoundry.sipxconfig.common.User) myObject;
    User api = (User) apiObject;//from  w w  w .  j  av a  2  s .co  m
    if (properties.contains(ALIASES_PROP) && !StringUtils.isBlank(my.getAliasesString())) {
        api.setAliases(my.getAliases().toArray(new String[0]));
    }
    if (properties.contains(GROUPS_PROP)) {
        Collection groupNames = CollectionUtils.collect(my.getGroups(), new NamedObject.ToName());
        api.setGroups((String[]) groupNames.toArray(new String[groupNames.size()]));
    }
    if (properties.contains(PERMISSIONS_PROP)) {
        Collection<String> permNames = my.getUserPermissionNames();
        if (!permNames.isEmpty()) {
            api.setPermissions(permNames.toArray(new String[permNames.size()]));
        }
    }
    if (properties.contains(BRANCH_NAME) && my.getBranch() != null) {
        api.setBranchName(my.getBranch().getName());
    }
}

From source file:org.sipfoundry.sipxconfig.backup.BackupConfig.java

void writePrimaryBackupConfig(Writer w, BackupPlan autoPlan, BackupPlan backupPlan, Collection<Location> hosts,
        BackupSettings auto, BackupSettings manual) throws IOException {
    YamlConfiguration config = new YamlConfiguration(w);

    config.startStruct("hosts");
    for (Location host : hosts) {
        config.write(host.getId().toString(), host.isPrimary() ? "127.0.0.1" : host.getAddress());
    }//from   w ww  .j  av  a2  s .  co m
    config.endStruct();

    config.write("plan", autoPlan.getType());
    config.write("max", autoPlan.getLimitedCount());
    config.startStruct("settings");
    config.startStruct(AUTO);
    writeSettings(config, autoPlan, auto);
    config.endStruct();
    config.startStruct(MANUAL);
    writeSettings(config, backupPlan, manual);
    config.endStruct();
    config.endStruct();

    config.startStruct("correlate_restore");
    for (Location host : hosts) {
        Collection<ArchiveDefinition> defs = m_backupManager.getArchiveDefinitions(host, null);
        @SuppressWarnings("unchecked")
        Collection<String> defIds = CollectionUtils.collect(defs, ArchiveDefinition.GET_IDS);
        config.writeInlineArray(host.getId().toString(), defIds);
    }
    config.endStruct();
}

From source file:org.sipfoundry.sipxconfig.common.DaoUtils.java

/**
 * Returns the collection of loaded hibernate beans
 *
 * @param hibernate hibernate template/*from w w w.  ja v  a2  s.  com*/
 * @param klass klass of the objects to be loaded
 * @param ids collection of object ids
 * @return newly created collection of objects loaded by hibernate
 */
public static Collection loadBeanByIds(HibernateTemplate hibernate, Class klass, Collection ids) {
    IdToBean idToBean = new IdToBean(hibernate, klass);
    return CollectionUtils.collect(ids, idToBean);
}

From source file:org.sipfoundry.sipxconfig.common.DaoUtils.java

/**
 * Performs operation on all the bean in the list. List of the bean is passed as list of ids,
 * beans are loaded by hibernate before operation starts.
 *
 * After operation is performed all the beans are saved.
 *//*  w  w  w  .j  ava  2 s . com*/
public static void doForAllBeanIds(HibernateTemplate hibernate, DaoEventPublisher eventPublisher,
        Transformer beanTransformer, Class klass, Collection ids) {
    IdToBean idToBean = new IdToBean(hibernate, klass);
    Transformer transformer = ChainedTransformer.getInstance(idToBean, beanTransformer);
    Collection beans = CollectionUtils.collect(ids, transformer);
    for (Object item : beans) {
        eventPublisher.publishSave(item);
    }
    hibernate.saveOrUpdateAll(beans);
}

From source file:org.sipfoundry.sipxconfig.components.TapestryContext.java

/**
 * Join a list of names objects into a string give a delimiter.
 *
 * Example: <code>/*from  w  w  w.ja v a  2s. c  o  m*/
 *  <span jwcid="@Insert" value="ognl:tapestry.joinNamed(items, ', ')"/>
 * </code>
 */
public String joinNamed(Collection namedItems, String delim) {
    Collection names = CollectionUtils.collect(namedItems, new NamedObject.ToName());
    return StringUtils.join(names.iterator(), delim);
}

From source file:org.sipfoundry.sipxconfig.dhcp.DhcpConfig.java

protected void writeConfig(Writer w, DhcpSettings settings, Address tftp, Address admin, Address staticHttp,
        Collection<Address> dns, Collection<Address> ntp) throws IOException {
    YamlConfiguration c = new YamlConfiguration(w);
    c.writeSettings(settings.getSettings().getSetting("dhcpd-config"));
    c.write("tftp", tftp != null ? tftp.getAddress() : "");
    c.write("config", admin.stripProtocol());
    c.write("statichttp", staticHttp.addressColonPort() + ":" + staticHttp.getCanonicalPort());
    c.writeInlineArray("ntp", CollectionUtils.collect(ntp, Address.GET_IP));
    c.writeInlineArray("dns", CollectionUtils.collect(dns, Address.GET_IP));
}

From source file:org.sipfoundry.sipxconfig.dialplan.DialPlanContextTestIntegration.java

public void testDuplicateDefaultRules() throws Exception {
    m_dialPlanSetup.setupDefaultRegion();
    List rules = m_dialPlanContext.getRules();
    Transformer bean2id = new BeanWithId.BeanToId();
    Collection ruleIds = CollectionUtils.collect(rules, bean2id);
    m_dialPlanContext.duplicateRules(ruleIds);
    commit();/* w  w  w.j a v  a  2 s  .c  o  m*/
    assertEquals(ruleIds.size() * 2, m_dialPlanContext.getRules().size());
    assertEquals(ruleIds.size() * 2, countRowsInTable("dialing_rule"));
}

From source file:org.sipfoundry.sipxconfig.feature.FeatureChangeRequest.java

@SuppressWarnings({ "unchecked", "rawtypes" })
Collection<Location> findLocationsByFeature(LocationFeature f, Map<Location, Set<LocationFeature>> map) {
    LocationByFeature findAndFilter = new LocationByFeature(f);
    Collection select = CollectionUtils.select(map.entrySet(), findAndFilter);
    Collection locations = CollectionUtils.collect(select, findAndFilter);
    return (Collection<Location>) locations;
}