Example usage for java.util TreeMap get

List of usage examples for java.util TreeMap get

Introduction

In this page you can find the example usage for java.util TreeMap get.

Prototype

public V get(Object key) 

Source Link

Document

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

Usage

From source file:org.squale.squaleweb.applicationlayer.action.export.ppt.AuditReportPPTData.java

/**
 * Add an array for the volumetry of projects by profile. The array contains: columns: {Profile, projectName1,
 * projectName2,..., projectNameN, Total, Comments} rows: {profile1, tre1OfProfile1, ..., treNOfProfileN, ...,
 * profileN, tre1OfprofileN, ..., treNOfProfileN}
 * //  w  w  w . j  ava  2  s. c om
 * @param slideToSet slide to modify
 * @param where place to add shape
 * @throws IOException if error
 * @throws PPTGeneratorException 
 */
public void setProfileVolTab(Slide slideToSet, Rectangle where) throws IOException, PPTGeneratorException {
    log.info("AuditReturn - setProfileVolTab");
    // Create a map of map of lists in order to create the volumetry profile array
    // key: profileName; value: mapTre
    // mapTre --> key: treName; value: List of String
    final int nbPredefinedCols = 2;
    final int nbCols = nbPredefinedCols + projectReports.size();
    TreeMap globalMap = new TreeMap();
    List titles = new ArrayList(nbCols);
    // col 1: profile
    titles.add(WebMessages.getString(request, "export.audit_report.set.volByProfile.profile_col.title"));
    for (int i = 0; i < projectReports.size(); i++) {
        ProjectReportDTO curProject = (ProjectReportDTO) projectReports.get(i);
        log.info("AuditReturn - setProfileVolTab Project n" + i + " : " + curProject.getName());
        // add project name on titles line
        titles.add(curProject.getName());
        TreeMap tresMap = (TreeMap) globalMap.get(curProject.getProfileName());
        if (null == tresMap) {
            tresMap = new TreeMap();
        }
        for (Iterator tresIt = curProject.getVolumetryMeasures().keySet().iterator(); tresIt.hasNext();) {
            String curTre = (String) tresIt.next();
            log.debug("AuditReturn - setProfileVolTab Measure : " + curTre);
            // add line in tab for this tre
            ArrayList valuesForTre = (ArrayList) tresMap.get(curTre);
            if (null == valuesForTre) {
                valuesForTre = createEmptyList(projectReports.size());
            }
            // set value for project cell for this tre
            valuesForTre.set(i, curProject.getVolumetryMeasures().get(curTre).toString());
            tresMap.put(curTre, valuesForTre);
        }
        log.debug("AuditReturn - setProfileVolTab Measure iteration done ");
        globalMap.put(curProject.getProfileName(), tresMap);
    }
    // add the two last titles
    titles.add(WebMessages.getString(request, "export.audit_report.set.volByProfile.total_col.title"));
    // create table with map
    // addPicture( slideToSet, htmlToImage( createVolByProfileTable( where, titles, globalMap ).toString() ), where
    // );
    log.debug("AuditReturn - setProfileVolTab end processing ");
    addHtmlPicture(slideToSet, createVolByProfileTable(where, titles, globalMap).toString(), where.x, where.y);
    log.info("AuditReturn - setProfileVolTab done");
}

From source file:com.clustercontrol.collect.composite.CollectSettingComposite.java

/**
 * ???????//from   w  w w . jav  a  2 s  .c  o  m
 *
 * @param managers ?????
 */
public void setCollectorItemCombo() {

    List<String> allItemList = new ArrayList<>();
    // ??????
    List<String> selectList = this.m_collectGraphView.getFacilityTreeComposite().getCheckedTreeInfo();
    TreeMap<String, List<String>> managerFacilityMap = new TreeMap<>();
    for (String selectStr : selectList) {
        String[] nodeDetail = selectStr.split(SEPARATOR_HASH_EX_HASH);
        if (nodeDetail.length != 0
                && nodeDetail[nodeDetail.length - 1].equals(String.valueOf(FacilityConstant.TYPE_NODE))) {
            String facilityId = nodeDetail[nodeDetail.length - 2];
            String managerName = nodeDetail[0];
            List<String> facilityList = managerFacilityMap.get(managerName);
            if (facilityList == null) {
                facilityList = new ArrayList<String>();
                managerFacilityMap.put(managerName, facilityList);
            }
            if (!facilityList.contains(facilityId)) {
                m_log.debug("????? managerName:" + managerName + ", facilityId:"
                        + facilityId);
                facilityList.add(facilityId);
            }
        }
    }

    for (Map.Entry<String, List<String>> map : managerFacilityMap.entrySet()) {
        String managerName = map.getKey();
        List<String> facilityList = map.getValue();
        // ???
        List<CollectKeyInfoPK> collectKeyInfoList;
        try {
            CollectEndpointWrapper wrapper = CollectEndpointWrapper.getWrapper(managerName);
            collectKeyInfoList = wrapper.getItemCodeList(facilityList);
        } catch (InvalidRole_Exception e) {
            m_log.warn("setCollectorItemCombo() getItemCodeList, " + e.getMessage());
            MessageDialog.openInformation(null, Messages.getString("message"),
                    Messages.getString("message.accesscontrol.16"));
            return;
        } catch (InvalidUserPass_Exception | HinemosUnknown_Exception e) {
            // ?
            m_log.warn("setCollectorItemCombo() getItemCodeList, " + e.getMessage(), e);
            MessageDialog.openInformation(null, Messages.getString("failed"),
                    Messages.getString("message.hinemos.failure.unexpected") + ", "
                            + HinemosMessage.replace(e.getMessage()));
            return;
        }
        // DB??????
        for (CollectKeyInfoPK collectKeyInfo : collectKeyInfoList) {
            String itemName = collectKeyInfo.getItemName();
            String monitorId = collectKeyInfo.getMonitorId();
            String displayName = collectKeyInfo.getDisplayName();
            if (!allItemList.contains(itemName + SEPARATOR_AT + displayName + SEPARATOR_AT + monitorId)) {
                // itemCode@itemName ????
                allItemList.add(itemName + SEPARATOR_AT + displayName + SEPARATOR_AT + monitorId);
            }
        }
    }

    // ??????????
    for (String itemCodeName : allItemList) {
        String itemName = HinemosMessage.replace(itemCodeName.split(SEPARATOR_AT)[0]);
        String displayName = itemCodeName.split(SEPARATOR_AT)[1];
        String monitorId = itemCodeName.split(SEPARATOR_AT)[2];
        // itemName?displayName???????
        if (!displayName.equals("") && !itemName.endsWith("[" + displayName + "]")) {
            itemName += "[" + displayName + "]";
        }
        String itemNameStr = itemName + "(" + monitorId + ")";
        if (!(Arrays.asList(m_listCollectorItem.getItems())).contains(itemNameStr)) {
            m_listCollectorItem.add(itemNameStr);
            m_listCollectorItem.setData(itemNameStr, itemCodeName);
        }
    }

    // ??
    setDefaultItemInfo();
}

From source file:com.sfs.whichdoctor.dao.RelationshipDAOImpl.java

/**
 * Given list of prior rotations GUID and build a supervisor map.
 *
 * @param priorRotations the prior rotations
 *
 * @return the tree map< integer, relationship bean>
 *//*from  w  ww . j a v  a 2s. com*/
private TreeMap<Integer, RelationshipBean> loadPriorSupervisorMap(
        final Collection<RotationBean> priorRotations) {
    TreeMap<Integer, RelationshipBean> supervisorMap = new TreeMap<Integer, RelationshipBean>();

    TreeMap<Long, RotationBean> orderedRotations = new TreeMap<Long, RotationBean>();

    for (RotationBean rotation : priorRotations) {
        long key = LARGE_LONG - rotation.getEndDate().getTime();
        orderedRotations.put(key, rotation);
    }

    for (Long key : orderedRotations.keySet()) {
        RotationBean rotation = orderedRotations.get(key);
        dataLogger.info("End date: " + rotation.getEndDate());
        if (supervisorMap.size() == 0) {
            supervisorMap = getSupervisorMap(rotation);
        }
    }
    return supervisorMap;
}

From source file:edu.utexas.cs.tactex.MarketManagerTest.java

/**
 * Testing the private method that learns
 * bid clearing distributions for the /*  w  ww  .  ja  v a  2  s. co  m*/
 * bidding MDP
 */
@Test
public void testRecordTradeResult() {
    marketManagerService.initialize(brokerContext);

    int tradeCreationTime = 1;

    double executionPrice = 2;
    double executionMWh = 3;
    marketManagerService.recordTradeResult(tradeCreationTime, 4, executionPrice, executionMWh);
    marketManagerService.recordTradeResult(tradeCreationTime, 5, executionPrice, executionMWh);
    marketManagerService.recordTradeResult(tradeCreationTime, 6, executionPrice, executionMWh);
    marketManagerService.recordTradeResult(tradeCreationTime, 10, executionPrice - 1, executionMWh);
    marketManagerService.recordTradeResult(tradeCreationTime, 10, executionPrice + 1, executionMWh);

    // test: trades are recorded
    @SuppressWarnings("unchecked")
    TreeMap<Integer, ArrayList<PriceMwhPair>> supportingBidGroups = (TreeMap<Integer, ArrayList<PriceMwhPair>>) ReflectionTestUtils
            .getField(marketManagerService, "supportingBidGroups");
    assertEquals("supportingBidGroups.size", 4, supportingBidGroups.size());
    assertEquals("supportingBidGroups.get(4).size", 1, supportingBidGroups.get(4).size());
    assertEquals("supportingBidGroups.get(5).size", 1, supportingBidGroups.get(5).size());
    assertEquals("supportingBidGroups.get(6).size", 1, supportingBidGroups.get(6).size());
    assertEquals("supportingBidGroups.get(10).size", 2, supportingBidGroups.get(10).size());
    assertEquals("supportingBidGroups(4,0).price", executionPrice,
            supportingBidGroups.get(4).get(0).getPricePerMwh(), 1e-6);
    assertEquals("supportingBidGroups(5,0).price", executionPrice,
            supportingBidGroups.get(5).get(0).getPricePerMwh(), 1e-6);
    assertEquals("supportingBidGroups(6,0).price", executionPrice,
            supportingBidGroups.get(6).get(0).getPricePerMwh(), 1e-6);
    assertEquals("supportingBidGroups(10,0).price is lowest", executionPrice - 1,
            supportingBidGroups.get(10).get(0).getPricePerMwh(), 1e-6);
    assertEquals("supportingBidGroups(10,1).price is higher", executionPrice + 1,
            supportingBidGroups.get(10).get(1).getPricePerMwh(), 1e-6);
    assertEquals("supportingBidGroups(4,0).mwh", executionMWh, supportingBidGroups.get(4).get(0).getMwh(),
            1e-6);
    assertEquals("supportingBidGroups(5,0).mwh", executionMWh, supportingBidGroups.get(5).get(0).getMwh(),
            1e-6);
    assertEquals("supportingBidGroups(6,0).mwh", executionMWh, supportingBidGroups.get(6).get(0).getMwh(),
            1e-6);
    assertEquals("supportingBidGroups(10,0).mwh", executionMWh, supportingBidGroups.get(10).get(0).getMwh(),
            1e-6);
    assertEquals("supportingBidGroups(10,1).mwh", executionMWh, supportingBidGroups.get(10).get(1).getMwh(),
            1e-6);
}

From source file:com.cmart.PageControllers.SellItemImagesController.java

/**
 * This method checks the page for any input errors that may have come from Client generator error
 * These would need to be check in real life to stop users attempting to hack and mess with things
 * /*  ww w.  j a v a2s .  c  o m*/
 * @param request
 * @author Andy (andrewtu@cmu.edu, turner.andy@gmail.com)
 */
public void checkInputs(HttpServletRequest request) {
    super.startTimer();

    if (request != null) {
        // If there is a multiform there are probably pictures
        if (ServletFileUpload.isMultipartContent(request)) {
            // Get the parameters
            try {
                // Create the objects needed to read the parameters
                factory = new DiskFileItemFactory();
                factory.setRepository(GV.LOCAL_TEMP_DIR);
                upload = new ServletFileUpload(factory);
                items = upload.parseRequest(request);
                Iterator<FileItem> iter = items.iterator();
                TreeMap<String, String> params = new TreeMap<String, String>();

                // Go through all the parameters and get the ones that are form fields
                while (iter.hasNext()) {
                    FileItem item = iter.next();

                    // If the item is a parameter, read it
                    if (item.isFormField()) {
                        params.put(item.getFieldName(), item.getString());
                    } else {
                        this.images.add(item);
                    }
                }

                /*
                 *  Get the parameters
                 */
                // Get the userID
                if (params.containsKey("userID")) {
                    try {
                        this.userID = Long.parseLong(params.get("userID"));

                        if (this.userID < 0)
                            if (!errors.contains(GlobalErrors.userIDLessThanZero))
                                errors.add(GlobalErrors.userIDLessThanZero);
                    } catch (NumberFormatException e) {
                        if (!errors.contains(GlobalErrors.userIDNotAnInteger))
                            errors.add(GlobalErrors.userIDNotAnInteger);

                    }
                } else {
                    if (!errors.contains(GlobalErrors.userIDNotPresent))
                        errors.add(GlobalErrors.userIDNotPresent);
                }

                // We nned to get the html5 tag as the parent cannot do the normal parsing
                if (params.containsKey("useHTML5")) {
                    try {
                        int u5 = Integer.parseInt(params.get("useHTML5"));
                        if (u5 == 1)
                            this.useHTML5 = true;
                        else
                            this.useHTML5 = false;
                    } catch (Exception e) {
                        this.useHTML5 = false;
                    }
                }

                // Get the authToken
                if (params.containsKey("authToken")) {
                    this.authToken = params.get("authToken");

                    if (this.authToken.equals(EMPTY))
                        if (!errors.contains(GlobalErrors.authTokenEmpty))
                            errors.add(GlobalErrors.authTokenEmpty);
                } else {
                    if (!errors.contains(GlobalErrors.authTokenNotPresent))
                        errors.add(GlobalErrors.authTokenNotPresent);
                }

                // Get the itemID
                if (params.containsKey("itemID")) {
                    try {
                        this.itemID = Long.parseLong(params.get("itemID"));

                        if (this.itemID <= 0)
                            if (!errors.contains(GlobalErrors.itemIDLessThanZero))
                                errors.add(GlobalErrors.itemIDLessThanZero);
                    } catch (NumberFormatException e) {
                        if (!errors.contains(GlobalErrors.itemIDNotAnInteger))
                            errors.add(GlobalErrors.itemIDNotAnInteger);
                    }
                } else {
                    if (!errors.contains(GlobalErrors.itemIDNotPresent))
                        errors.add(GlobalErrors.itemIDNotPresent);
                }
            } catch (FileUploadException e1) {
                // TODO Auto-generated catch block
                //System.out.println("SellItemImageController (checkInputs): There was an error in the multi-form");
                e1.printStackTrace();
            }
        }
        // Do normal request processing
        else {
            super.checkInputs(request);

            // Get the userID (if exists), we will pass it along to the next pages
            try {
                this.userID = CheckInputs.checkUserID(request);
            } catch (Error e) {
                // The user must be logged in to upload the images
                if (!errors.contains(e))
                    errors.add(e);
            }

            // Get the authToken (if exists), we will pass it along to the next pages
            try {
                this.authToken = CheckInputs.checkAuthToken(request);
            } catch (Error e) {
                if (!errors.contains(e))
                    errors.add(e);
            }

            // Get the itemID 
            try {
                this.itemID = CheckInputs.checkItemID(request);

                if (this.itemID <= 0)
                    if (!errors.contains(GlobalErrors.itemIDLessThanZero))
                        errors.add(GlobalErrors.itemIDLessThanZero);
            } catch (Error e) {
                if (!errors.contains(e))
                    errors.add(e);

                this.itemID = -1;
            }
        }
    }

    // Calculate how long that took
    super.stopTimerAddParam();
}

From source file:de.uzk.hki.da.sb.SIPFactory.java

/**
 * Creates a list of source folders// w  w  w .j a  v a2  s.  c  o m
 * 
 * @param folderPath
 *            The main source folder path
 * @throws Exception
 */

HashMap<File, String> createFolderList(String folderPath) throws Exception {

    HashMap<File, String> folderListWithFolderNames = new HashMap<File, String>();
    File sourceFolder = new File(folderPath);

    switch (kindofSIPBuilding) {
    case MULTIPLE_FOLDERS:
        List<File> folderContent = Arrays.asList(sourceFolder.listFiles());
        for (File file : folderContent) {
            if (!file.isHidden() && file.isDirectory())
                folderListWithFolderNames.put(file, null);
        }
        break;

    case SINGLE_FOLDER:
        folderListWithFolderNames.put(sourceFolder, null);
        break;

    case NESTED_FOLDERS:
        NestedContentStructure ncs;
        try {
            TreeMap<File, String> metadataFileWithType = new FormatDetectionService(sourceFolder)
                    .getMetadataFileWithType();
            if (!metadataFileWithType.isEmpty() && (!metadataFileWithType.get(metadataFileWithType.firstKey())
                    .equals(C.CB_PACKAGETYPE_METS))) {
                messageWriter.showMessage("Es wurde eine Metadatendatei des Typs "
                        + metadataFileWithType.get(metadataFileWithType.firstKey())
                        + " auf der obersten Ebene gefunden. "
                        + "\nBitte whlen Sie diese Option ausschlielich fr die Erstellung von SIPs des Typs METS.");
            } else {
                ncs = new NestedContentStructure(sourceFolder);
                folderListWithFolderNames = ncs.getSipCandidates();
                if (folderListWithFolderNames.isEmpty()) {
                    messageWriter.showMessage(
                            "Es wurde kein Unterverzeichnis mit einer METS-Metadatendatei gefunden.");
                }
                break;
            }
        } catch (IOException e) {
            throw new Exception(e);
        }
    default:
        break;
    }

    return folderListWithFolderNames;
}

From source file:org.eclipse.january.dataset.Stats.java

static double[] outlierValuesMap(final Dataset a, int nl, int nh) {
    final TreeMap<Double, Integer> lMap = new TreeMap<Double, Integer>();
    final TreeMap<Double, Integer> hMap = new TreeMap<Double, Integer>();

    int ml = 0;//from   w ww . ja v a2s .c  o m
    int mh = 0;
    IndexIterator it = a.getIterator();
    while (it.hasNext()) {
        Double x = a.getElementDoubleAbs(it.index);
        Integer i;
        if (ml == nl) {
            Double k = lMap.lastKey();
            if (x < k) {
                i = lMap.get(k) - 1;
                if (i == 0) {
                    lMap.remove(k);
                } else {
                    lMap.put(k, i);
                }
                i = lMap.get(x);
                if (i == null) {
                    lMap.put(x, 1);
                } else {
                    lMap.put(x, i + 1);
                }
            }
        } else {
            i = lMap.get(x);
            if (i == null) {
                lMap.put(x, 1);
            } else {
                lMap.put(x, i + 1);
            }
            ml++;
        }

        if (mh == nh) {
            Double k = hMap.firstKey();
            if (x > k) {
                i = hMap.get(k) - 1;
                if (i == 0) {
                    hMap.remove(k);
                } else {
                    hMap.put(k, i);
                }
                i = hMap.get(x);
                if (i == null) {
                    hMap.put(x, 1);
                } else {
                    hMap.put(x, i + 1);
                }
            }
        } else {
            i = hMap.get(x);
            if (i == null) {
                hMap.put(x, 1);
            } else {
                hMap.put(x, i + 1);
            }
            mh++;
        }
    }

    // Attempt to make values distinct
    double lx = lMap.lastKey();
    double hx = hMap.firstKey();
    if (lx >= hx) {
        Double h = hMap.higherKey(lx);
        if (h != null) {
            hx = h;
            mh--;
        } else {
            Double l = lMap.lowerKey(hx);
            if (l != null) {
                lx = l;
                ml--;
            }
        }

    }
    return new double[] { lMap.lastKey(), hMap.firstKey(), ml, mh };
}

From source file:org.gvsig.framework.web.service.impl.OGCInfoServiceImpl.java

public ServiceMetadata getMetadataInfoFromWMS(String urlServer) {

    ServiceMetadata servMetadata = new ServiceMetadata();

    try {//from  w w w. j a  v a  2  s.c  o m
        WMSClient wms = new WMSClient(urlServer);
        wms.connect(null);

        // set server information
        WMSServiceInformation serviceInfo = wms.getServiceInformation();

        servMetadata.setAbstractStr(serviceInfo.abstr);
        servMetadata.setFees(serviceInfo.fees);
        servMetadata.setKeywords(serviceInfo.keywords);
        servMetadata.setName(serviceInfo.name);
        servMetadata.setTitle(serviceInfo.title);
        servMetadata.setUrl(urlServer);
        servMetadata.setVersion(serviceInfo.version);
        // I can't get from service the accessConstraints value
        //servMetadata.setAccessConstraints(accessConstraints);

        // get layers
        TreeMap layers = wms.getLayers();
        if (layers != null) {
            List<Layer> lstLayers = new ArrayList<Layer>();
            Set<String> keys = layers.keySet();
            for (String key : keys) {
                WMSLayer wmsLayer = (WMSLayer) layers.get(key);
                Layer layer = new Layer();
                layer.setName(wmsLayer.getName());
                layer.setTitle(wmsLayer.getTitle());
                lstLayers.add(layer);
            }
            servMetadata.setLayers(lstLayers);
        }

        // get operations
        Hashtable supportedOperations = serviceInfo.getSupportedOperationsByName();
        if (supportedOperations != null) {
            List<OperationsMetadata> lstOperations = new ArrayList<OperationsMetadata>();
            Set<String> keys = supportedOperations.keySet();
            for (String key : keys) {
                OperationsMetadata opMetadata = new OperationsMetadata();
                opMetadata.setName(key);
                opMetadata.setUrl((String) supportedOperations.get(key));
                lstOperations.add(opMetadata);
            }
            servMetadata.setOperations(lstOperations);
        }
        // get contact address
        ContactAddressMetadata contactAddress = null;
        if (StringUtils.isNotEmpty(serviceInfo.address) || StringUtils.isNotEmpty(serviceInfo.addresstype)
                || StringUtils.isNotEmpty(serviceInfo.place) || StringUtils.isNotEmpty(serviceInfo.country)
                || StringUtils.isNotEmpty(serviceInfo.postcode)
                || StringUtils.isNotEmpty(serviceInfo.province)) {
            contactAddress = new ContactAddressMetadata();
            contactAddress.setAddress(serviceInfo.address);
            contactAddress.setAddressType(serviceInfo.addresstype);
            contactAddress.setCity(serviceInfo.place);
            contactAddress.setCountry(serviceInfo.country);
            contactAddress.setPostCode(serviceInfo.postcode);
            contactAddress.setStateProvince(serviceInfo.province);
        }

        // get contact info
        ContactMetadata contactMetadata = null;
        if (contactAddress != null || StringUtils.isNotEmpty(serviceInfo.email)
                || StringUtils.isNotEmpty(serviceInfo.fax) || StringUtils.isNotEmpty(serviceInfo.organization)
                || StringUtils.isNotEmpty(serviceInfo.personname)
                || StringUtils.isNotEmpty(serviceInfo.function) || StringUtils.isNotEmpty(serviceInfo.phone)) {
            contactMetadata = new ContactMetadata();
            contactMetadata.setContactAddress(contactAddress);
            contactMetadata.setEmail(serviceInfo.email);
            contactMetadata.setFax(serviceInfo.fax);
            contactMetadata.setOrganization(serviceInfo.organization);
            contactMetadata.setPerson(serviceInfo.personname);
            contactMetadata.setPosition(serviceInfo.function);
            contactMetadata.setTelephone(serviceInfo.phone);
        }
        servMetadata.setContact(contactMetadata);

    } catch (Exception exc) {
        // Show exception in log
        logger.error("Exception on getMetadataInfoFromWMS", exc);
        throw new ServerGeoException();
    }

    return servMetadata;
}

From source file:crawler.HackerEarthCrawler.java

@Override
public void crawl() {

    int flag = 0;

    //set of urls which should be crawled
    TreeSet<String> linksset = new TreeSet<String>();
    TreeSet<String> tempset = new TreeSet<String>();
    TreeSet<String> tutorialset = new TreeSet<String>();
    //final set of problem urls
    TreeSet<String> problemset = new TreeSet<String>();
    //visited for maintaing status of if url is already crawled or not
    TreeMap<String, Integer> visited = new TreeMap<String, Integer>();

    //add base url
    linksset.add(baseUrl);//from   ww  w.j a v a  2 s  .  c  om
    //mark base url as not crawled
    visited.put(baseUrl, 0);

    try {
        while (true) {
            flag = 0;
            tempset.clear();

            for (String str : linksset) {
                //check if url is already crawled or not and it has valid domain name
                if ((visited.get(str) == 0) && (str.startsWith("https://www.hackerearth.com/"))) {
                    System.out.println("crawling  " + str);

                    //retriving response of current url as document
                    Document doc = Jsoup.connect(str).timeout(0).userAgent(
                            "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0")
                            .referrer("http://www.google.com").ignoreHttpErrors(true).get();
                    //retriving all urls from current page
                    Elements links = doc.select("a[href]");

                    //mark url as crawled
                    visited.put(str, 1);

                    //mark flag as url is crawled
                    flag = 1;
                    //retrive all urls
                    for (Element link : links) {
                        if (link.absUrl("href").endsWith("/tutorial/")) {
                            tutorialset.add(link.absUrl("href"));
                        }
                        //check if url is problem url then add it in problemurlset
                        if (link.absUrl("href").startsWith("https://www.hackerearth.com/")
                                && isProblemUrl(link.absUrl("href"))) {
                            problemset.add(link.absUrl("href"));
                        }
                        //check if url has valid domain and it has problem urls or not
                        if (link.absUrl("href").contains(("https://www.hackerearth.com/"))
                                && isCrawlable(link.absUrl("href"))) {
                            //if link is not visited then mark it as uncrawled
                            if (!visited.containsKey(link.absUrl("href"))) {
                                visited.put(link.absUrl("href"), 0);
                            }
                            //add it in tempsetorary set
                            tempset.add(link.absUrl("href"));
                            //System.out.println("\n  base: "+str+" ::: link  : " + link.absUrl("href"));
                        }
                    }
                }
            }
            //if nothing is left to crawl break the loop
            if (flag == 0) {
                break;
            }
            //add all retrieved links to linksset
            linksset.addAll(tempset);
        }

        System.out.println("\n\ntotal problem urls " + problemset.size());

        int i = 0;
        for (String str : problemset) {
            System.out.println("link " + i + " : " + str);
            i++;
        }

    } catch (IOException ex) {
        Logger.getLogger(HackerEarthCrawler.class.getName()).log(Level.SEVERE, null, ex);
    }

    //scrap and store into database
    //for every problem url scrap problem page
    for (String problemUrl : problemset) {

        System.out.println("problemUrl :" + problemUrl);
        try {
            //create problem class to store in database
            Problem problem = new Problem();
            String problemSIOC = "", problemIOC = "";
            String problemTitle = "", problemStatement = "", problemInput = "", problemOutput = "",
                    problemConstraints = "";
            String sampleInput = "", sampleOutput = "";
            String problemExplanation = "";
            //set default timelimit to 1 second
            double problemTimeLimit = 1.0;
            ArrayList<String> tags = new ArrayList<String>();

            //get response for given problem url
            Response response = Jsoup.connect(problemUrl).execute();
            Document doc = response.parse();

            //retrieve problem title from page
            Element elementTitle = doc.getElementsByTag("title").first();
            StringTokenizer stTitle = new StringTokenizer(elementTitle.text(), "|");
            problemTitle = stTitle.nextToken().trim();

            Element content = doc.getElementsByClass("starwars-lab").first();
            problemSIOC = content.text();
            Elements e = content.children();

            //to find problem statement
            String breakloop[] = { "input", "input:", "input :", "input format:", "input format :",
                    "input format", "Input and output", "constraints :", "constraints:", "constraints",
                    "$$Input :$$" };
            flag = 0;
            for (Element p : e) {
                String tempStatement = "";
                for (Element pp : p.getAllElements()) {

                    for (String strbreak : breakloop) {
                        if (StringUtils.equalsIgnoreCase(pp.ownText(), strbreak)) {
                            //System.out.println("strbreak :"+strbreak);

                            tempStatement = p.text().substring(0,
                                    p.text().toLowerCase().indexOf(strbreak.toLowerCase()));
                            // System.out.println("temp "+tempStatement);
                            flag = 1;
                            break;
                        }
                    }
                }

                if (flag == 1) {
                    problemStatement += tempStatement;
                    //remove extra space at end
                    if (tempStatement.length() == 0) {
                        problemStatement = problemStatement.substring(0, problemStatement.length() - 1);
                    }
                    break;
                }
                problemStatement += p.text() + " ";
            }

            System.out.println("problemSIOC :" + problemSIOC);
            System.out.println("problemStatement :" + problemStatement);

            if (problemStatement.length() <= problemSIOC.length()) {
                //remove problem statement from whole text and remove extra spaces at the beginning and the end
                problemIOC = problemSIOC.substring(problemStatement.length()).trim();
            } else {
                problemIOC = "";
            }

            System.out.println("problemIOC :" + problemIOC);

            //keywords for identifying input
            String decideInput[] = { "Input format :", "Input format:", "Input format", "inputformat:",
                    "inputformat :", "inputformat", "input and output", "input :", "input:", "input" };
            //keywords for identifying output
            String decideOutput[] = { "output format :", "output format:", "Output format", "outputformat:",
                    "outputformat :", "outputformat", "output :", "output:", "output" };
            //keywords for identifying constraint
            String decideConstraint[] = { "constraints:", "constraints :", "constraints", "Constraints :",
                    "constraint:", "constraint :", "constraint", "Contraints :" };

            int posin = 0, posoutput = 0, poscon = 0, idxin, idxout, idxcon, flaginput = 0, flagoutput = 0,
                    flagcon = 0, inlen = 0, outlen = 0, conlen = 0;

            //find inputformat position,length of keyword
            for (idxin = 0; idxin < decideInput.length; idxin++) {
                if (StringUtils.containsIgnoreCase(problemIOC, decideInput[idxin])) {

                    posin = problemIOC.toLowerCase().indexOf(decideInput[idxin].toLowerCase());
                    flaginput = 1;
                    inlen = decideInput[idxin].length();

                    //decide it is keyowrd for actucal input or it is "sample input"
                    if (StringUtils.containsIgnoreCase(problemIOC, "sample input")) {
                        if (posin > problemIOC.toLowerCase().indexOf("sample input")) {
                            flaginput = 0;
                            inlen = 0;
                        } else {
                            break;
                        }
                    } else {
                        break;
                    }
                }
            }

            //find outputformat position,length of keyword
            for (idxout = 0; idxout < decideOutput.length; idxout++) {
                if (StringUtils.containsIgnoreCase(problemIOC, decideOutput[idxout])) {
                    posoutput = problemIOC.toLowerCase().indexOf(decideOutput[idxout].toLowerCase());
                    flagoutput = 1;
                    outlen = decideOutput[idxout].length();
                    break;
                }
            }

            //find constraint position,length of keyword
            for (idxcon = 0; idxcon < decideConstraint.length; idxcon++) {
                if (StringUtils.containsIgnoreCase(problemIOC, decideConstraint[idxcon])) {
                    poscon = problemIOC.toLowerCase().indexOf(decideConstraint[idxcon].toLowerCase());
                    flagcon = 1;
                    conlen = decideConstraint[idxcon].length();
                    break;
                }
            }

            System.out.println("input " + flaginput + " " + inlen + " " + posin);
            System.out.println("output " + flagoutput + " " + outlen + " " + posoutput);
            System.out.println("constraint " + flagcon + " " + conlen + " " + poscon);
            //retrieve problem input and output if present in problem page

            //if input format is present
            if (flaginput == 1) {
                //if input keyword is "input and output" and contraint is present in problem page
                if (idxin == 6 && flagcon == 1) {
                    problemInput = problemIOC.substring(inlen, poscon);
                }
                //if input keyword is "input and output" and contraint is not present in problem page
                else if (idxin == 6 && flagcon == 0) {
                    problemInput = problemIOC.substring(inlen);
                }
                //if output format and constraint is present
                else if (flagoutput == 1 && flagcon == 1) {
                    //if constraint is present before input format
                    if (poscon < posin) {
                        problemInput = problemIOC.substring(posin + inlen, posoutput);
                        problemOutput = problemIOC.substring(posoutput + outlen);
                    }
                    //if constraint is present before sample
                    else if (poscon < posoutput) {
                        problemInput = problemIOC.substring(inlen, poscon);
                        problemOutput = problemIOC.substring(posoutput + outlen);
                    } else {
                        problemInput = problemIOC.substring(inlen, posoutput);
                        problemOutput = problemIOC.substring(posoutput + outlen, poscon);
                    }
                }
                //if constraint is not present
                else if (flagoutput == 1 && flagcon == 0) {
                    problemInput = problemIOC.substring(inlen, posoutput);
                    problemOutput = problemIOC.substring(posoutput + outlen);
                } else if (flagoutput == 0 && flagcon == 1) {
                    if (poscon < posin) {
                        problemInput = problemIOC.substring(posin + inlen);
                    } else {
                        problemInput = problemIOC.substring(poscon + conlen, posin);
                    }
                    problemOutput = "";
                } else {
                    problemInput = problemIOC.substring(inlen);
                    problemOutput = "";
                }
            }
            //if input format and output format is not present
            else {
                problemInput = "";
                problemOutput = "";
            }

            //if constraint is present
            if (flagcon == 1) {
                //if constraint is present before input format
                if (poscon < posin) {
                    problemConstraints = problemIOC.substring(0, posin);
                }
                //if constraint is present before output format
                else if (poscon < posoutput) {
                    problemConstraints = problemIOC.substring(poscon + conlen, posoutput);
                } else {
                    problemConstraints = problemIOC.substring(poscon + conlen);
                }
            }

            System.out.println("problemInput :" + problemInput);
            System.out.println("problemOutput :" + problemOutput);
            System.out.println("problemConstraints :" + problemConstraints);

            //retrieve problem tags from problem page
            Element elementtag = doc.getElementsByClass("problem-tags").first().child(1);
            StringTokenizer st = new StringTokenizer(elementtag.text(), ",");
            while (st.hasMoreTokens()) {
                tags.add(st.nextToken().trim());
            }

            //retrieve sample input sample output if present
            Element elementSIO = doc.getElementsByClass("input-output-container").first();
            //if sample input output is present
            if (elementSIO != null) {
                //find position of sample output
                int soutpos = elementSIO.text().indexOf("SAMPLE OUTPUT");
                sampleInput = elementSIO.text().substring(12, soutpos);
                sampleOutput = elementSIO.text().substring(soutpos + 13);
                System.out.println("Sample input :\n" + sampleInput + "\n\n\n");
                System.out.println("Sample Output :\n" + sampleOutput);
            } else {
                sampleInput = "";
                sampleOutput = "";
            }

            //retrieve problem explanation from problem page if present
            Element elementExplanation = doc.getElementsByClass("standard-margin").first().child(0);
            if (elementExplanation.text().toLowerCase().contains("explanation")) {
                problemExplanation = elementExplanation.nextElementSibling().text();
            }
            System.out.println("Explanation :" + problemExplanation);

            //retrieve timelimit
            Element elementTL = doc.getElementsByClass("problem-guidelines").first().child(0).child(1);
            StringTokenizer stTL = new StringTokenizer(elementTL.ownText(), " ");
            problemTimeLimit = Double.parseDouble(stTL.nextToken());

            //System.out.println("problemTimeLimit :"+problemTimeLimit);
            //set all retrieved information to problem class
            problem.setProblemUrl(problemUrl);
            if (problemTitle.length() == 0) {
                problemTitle = null;
            }
            if (problemStatement.length() == 0) {
                problemStatement = null;
            }
            if (problemInput.length() == 0) {
                problemInput = null;
            }
            if (problemOutput.length() == 0) {
                problemOutput = null;
            }
            if (problemExplanation.length() == 0) {
                problemExplanation = null;
            }
            if (problemConstraints.length() == 0) {
                problemConstraints = null;
            }
            problem.setTitle(problemTitle);
            problem.setProblemUrl(problemUrl);
            problem.setProblemStatement(problemStatement);
            problem.setInputFormat(problemInput);
            problem.setOutputFormat(problemOutput);
            problem.setTimeLimit(problemTimeLimit);
            problem.setExplanation(problemExplanation);
            problem.setConstraints(problemConstraints);

            //set sample input output to problem class
            SampleInputOutput sampleInputOutput = new SampleInputOutput(problem, sampleInput, sampleOutput);
            problem.getSampleInputOutputs().add(sampleInputOutput);
            //set platform as hackerearth
            problem.setPlatform(Platform.HackerEarth);
            for (String strtag : tags) {
                problem.getTags().add(strtag);
            }

            //store in database
            Session session = null;
            Transaction transaction = null;
            try {
                //start session
                session = HibernateUtil.getSessionFactory().openSession();
                transaction = session.beginTransaction();

                //check if problem is already stored in database
                String hql = "FROM Problem p where p.problemUrl = :problem_url";
                Problem oldProblem = (Problem) session.createQuery(hql).setString("problem_url", problemUrl)
                        .uniqueResult();
                String task;

                //if problem is present in database
                if (oldProblem != null) {
                    //update the old problem
                    task = "updated";
                    //retrieve id of old problem
                    problem.setId(oldProblem.getId());
                    session.delete(oldProblem);
                    session.flush();
                    session.save(problem);
                } else {
                    task = "saved";
                    session.save(problem);
                }

                transaction.commit();
                //log the info to console
                Logger.getLogger(CodeForcesCrawler.class.getName()).log(Level.INFO, "{0} {1}",
                        new Object[] { task, problem.getProblemUrl() });
            } catch (HibernateException ee) {
                if (transaction != null) {
                    transaction.rollback();
                }
                Logger.getLogger(CodeForcesCrawler.class.getName()).log(Level.SEVERE,
                        "Cannot Insert/Update problem into databse: " + problemUrl, e);
            } finally {
                //close the session
                if (session != null) {
                    session.close();
                }
            }
        } catch (Exception ee) {
            System.out.println(ee.toString());
        }
    }

    System.out.println("\n\n\n\ntutorial urls\n\n");
    try {

        for (String tutorialurl : tutorialset) {
            //System.out.println(tutorialurl+"\n\n");
            Response tutorialres = Jsoup.connect(tutorialurl).execute();
            Document doc = tutorialres.parse();

            Tutorial tutorial = new Tutorial();
            tutorial.setContent(doc.getElementsByClass("tutorial").first().text());

            tutorial.setName(baseUrl);
            tutorialurl = tutorialurl.substring(0, tutorialurl.length() - 10);
            StringTokenizer tutorialtok = new StringTokenizer(tutorialurl, "/");

            String tempstr = "";
            while (tutorialtok.hasMoreTokens()) {
                tempstr = tutorialtok.nextToken();
            }

            Session session = null;
            Transaction transaction = null;
            try {
                //start session
                session = HibernateUtil.getSessionFactory().openSession();
                transaction = session.beginTransaction();

                //check if problem is already stored in database
                String hql = "FROM Tutorial p where p.name = :name";
                Tutorial oldProblem = (Tutorial) session.createQuery(hql).setString("name", tempstr)
                        .uniqueResult();
                String task;

                //if problem is present in database
                if (oldProblem != null) {
                    //update the old problem
                    task = "updated";
                    //retrieve id of old problem
                    tutorial.setName(oldProblem.getName());
                    session.delete(oldProblem);
                    session.flush();
                    session.save(tutorial);
                } else {
                    task = "saved";
                    tutorial.setName(tempstr);
                    session.save(tutorial);
                }

                transaction.commit();
                //log the info to console
                Logger.getLogger(CodeForcesCrawler.class.getName()).log(Level.INFO, "{0} {1}",
                        new Object[] { task, tutorial.getName() });
            } catch (HibernateException ee) {
                if (transaction != null) {
                    transaction.rollback();
                }
                Logger.getLogger(CodeForcesCrawler.class.getName()).log(Level.SEVERE,
                        "Cannot Insert/Update problem into databse: " + tempstr, ee);
            } finally {
                //close the session
                if (session != null) {
                    session.close();
                }
            }

        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

From source file:gemlite.shell.admin.dao.AdminDao.java

public void Import() throws IOException {
    TreeMap<String, String> memberTreeMap = new TreeMap<String, String>();
    String memberId = showMembers(memberTreeMap);
    if ("X".equalsIgnoreCase(memberId) || StringUtils.isEmpty(memberId))
        return;//from  w  ww  . j  a va2 s  .c  o  m
    String regionName = showRegions();
    if ("X".equalsIgnoreCase(regionName) || StringUtils.isEmpty(regionName))
        return;
    String filePath = showFilePath();
    if ("X".equalsIgnoreCase(filePath))
        return;
    String showLog = showLog();
    if ("X".equalsIgnoreCase(showLog))
        return;
    Map param = new HashMap();
    param.put("beanName", "ImportDataService");

    Map args = new HashMap();
    args.put("REGIONPATH", regionName);
    args.put("FILEPATH", filePath);
    args.put("showLog", showLog);
    args.put("IP", memberTreeMap.get(memberId));
    args.put("MEMBERID", memberId);
    param.put("userArgs", args);

    LogUtil logUtil = LogUtil.newInstance();
    try {
        // ?batch?
        // CoreFunctions.batchModeOn();
        Execution execution = FunctionService.onServer(clientPool).withArgs(param)
                .withCollector(new PrintResultCollector());
        ResultCollector rc = execution.execute("REMOTE_ADMIN_FUNCTION");
        rc.getResult();
        // batch?
        // CoreFunctions.batchModeOff();
    } catch (Exception e) {
        // CoreFunctions.batchModeOff();
        e.printStackTrace();
    }
    long cost = logUtil.cost();
    System.out.println("Import data total cost:" + cost + " min: " + (cost / (1000 * 60)) + " minutes");
}