protected WireFeed createRealFeed(String type, SyndFeed syndFeed) {
    Channel channel = new Channel(type);
    channel.setModules(ModuleUtils.cloneModules(syndFeed.getModules()));

    channel.setEncoding(syndFeed.getEncoding());

    channel.setTitle(syndFeed.getTitle());
    channel.setLink(syndFeed.getLink());
    channel.setDescription(syndFeed.getDescription());
    SyndImage sImage = syndFeed.getImage();
    if (sImage != null) {
      channel.setImage(createRSSImage(sImage));
    }

    List sEntries = syndFeed.getEntries();
    if (sEntries != null) {
      channel.setItems(createRSSItems(sEntries));
    }
    return channel;
  }
예제 #2
0
파일: Rss.java 프로젝트: keithmasters/zeprs
  /**
   * Exports a list of patients at the clinic and their records. Compare a list of all patients in
   * the rss file last published by the server with a list of patients in the system. Publish any
   * changed records if they are not equal If site is the master data center (such as ZEPRS), loops
   * through each site's patient list and creates RSS file for each site. Note that a patient's xml
   * record could be listed in more than one site - The patient listing for each site selects from
   * patient_status.site_id. Download the remote site listing and see if there are adidtional
   * patients who may have been transferred to another site - UTH for instance. This enables sites
   * to see records of their regular patients who were referred to another site. Also creates an
   * index which is used on remote sites for finding patient records within the imported archive.
   *
   * @param conn
   * @param siteAbbrev - if siteabbrev = ZEP,, export all sites w/ their own rss feed.
   * @param siteId
   * @param checkXml - true if you want to check if xml files have been generated.
   * @return
   * @throws SQLException
   * @throws ServletException
   * @throws ObjectNotFoundException
   * @throws IOException
   * @throws ClassNotFoundException
   * @throws IllegalAccessException
   * @throws PersistenceException
   * @throws InvocationTargetException
   * @throws FeedException
   */
  public static String createSiteRssFiles(
      Connection conn, String siteAbbrev, Long siteId, Boolean checkXml)
      throws SQLException, ServletException, ObjectNotFoundException, IOException,
          ClassNotFoundException, IllegalAccessException, PersistenceException,
          InvocationTargetException, FeedException {
    String message = null;
    StringBuffer sbuf = new StringBuffer();
    Timestamp beginDate = null;
    Timestamp endDate = new Timestamp(System.currentTimeMillis());
    Map statusMap = DynaSiteObjects.getStatusMap();
    Publisher publisher = null;
    String publisherFile = Constants.ARCHIVE_PATH + "publisher.xml";
    String ipAddress = null;
    Boolean renderAllSites = false;
    try {
      publisher = (Publisher) XmlUtils.getOne(publisherFile);
      ipAddress = publisher.getUrl();
    } catch (FileNotFoundException e) {
      log.debug("publisher.xml not available - cannot render site rss files.");
    }

    if (publisher != null) {
      // check the db for the date of the last archive.
      // Fetch list of updated patients. This list is used to generate each patient's updated xml
      // file
      ResultSet updatedPatients = null;
      try {
        ArchiveLog mostRecentArchive = (ArchiveLog) ArchiveDAO.getOne(conn);
        beginDate = mostRecentArchive.getArchived();
        updatedPatients = PatientDAO.getAllDate(conn, beginDate, endDate);
      } catch (SQLException e) {
        e.printStackTrace();
      } catch (ServletException e) {
        e.printStackTrace();
      } catch (ObjectNotFoundException e) {
        // This is the first time this has been run - archive all.
        updatedPatients = PatientDAO.getAllRS(conn);
        renderAllSites = true;
      }

      // update date of last archive before any more records get updated.
      Long archiveId = (Long) ArchiveDAO.save(conn, endDate, (long) 1);
      HashMap updatedMap = new HashMap();
      while (updatedPatients.next()) {
        Long updatedId = updatedPatients.getLong("id");
        updatedMap.put(updatedId, updatedId);
      }
      List<Site> clinics = null;

      if (siteAbbrev.equals("ZEP")) {
        // loop thorough all of the sites and render an rss file for each one.
        clinics = DynaSiteObjects.getClinics();
        // todo: reset to the big list - preset to KAL and AIR
        /*clinics = new ArrayList<Site>();
        Site site = (Site) DynaSiteObjects.getClinicMap().get(Long.valueOf(16));
        clinics.add(site);
        site = (Site) DynaSiteObjects.getClinicMap().get(Long.valueOf(1));
        clinics.add(site);*/
      } else {
        clinics = new ArrayList<Site>();
        Site site = (Site) DynaSiteObjects.getClinicMap().get(siteId);
        clinics.add(site);
      }
      // create the archive listing
      // StringBuffer indexBuffer = new StringBuffer();
      HashMap<String, String> indexMap = new HashMap<String, String>();
      int currentSiteNum = 0;
      int archivedPatientsAllSites = 0;
      for (Site site : clinics) {
        currentSiteNum++;
        int archivedPatientsThisSite = 0;
        siteAbbrev = site.getAbbreviation();
        siteId = site.getId();
        String rssFileName =
            org.cidrz.webapp.dynasite.Constants.ARCHIVE_PATH
                + siteAbbrev
                + Constants.pathSep
                + "rss.xml";
        Timestamp lastBuildDate = new Timestamp(System.currentTimeMillis());
        Channel feed = new Channel();
        String feedType = "rss_2.0";
        feed.setFeedType(feedType);
        feed.setDescription(siteAbbrev + " Patients");
        String port = Constants.APP_PORT;
        feed.setLink(
            "http://" + ipAddress + ":" + port + "/" + "archive/" + siteAbbrev + "/rss.xml");
        feed.setTitle(siteAbbrev); // Title is used to identify which site the feed is from.
        feed.setLastBuildDate(lastBuildDate);
        List entries = new ArrayList();
        // Loop through the list of all patients and see if their id exists in the updatedMap
        ResultSet patientListResultSet = PatientDAO.getAllSiteRS(conn, siteId);
        ArrayList<Patient> patientRssList = new ArrayList<Patient>();
        while (patientListResultSet.next()) {
          Long patientId = patientListResultSet.getLong("id");
          String zeprsId = patientListResultSet.getString("district_patient_id");
          String surname = patientListResultSet.getString("surname");
          Timestamp lastModified = patientListResultSet.getTimestamp("last_modified");
          Long clinicId =
              patientListResultSet.getLong(
                  "site_id"); // clinic the patient belongs to - should be the same as the site
                              // feed.
          Patient rssPatient = new Patient();
          rssPatient.setId(patientId);
          rssPatient.setDistrictPatientid(zeprsId);
          rssPatient.setSurname(surname);
          rssPatient.setLastModified(lastModified);
          rssPatient.setSiteId(clinicId);
          patientRssList.add(rssPatient);
        }
        HashMap<String, String> patientRssMap = new HashMap<String, String>();
        // loop through the patientRssList to weed out duplicates
        for (Patient patient : patientRssList) {
          String zeprsId = patient.getDistrictPatientid();
          patientRssMap.put(zeprsId, zeprsId);
        }
        String rssMessage =
            "Generating RSS and Patient Xml for "
                + siteAbbrev
                + " :"
                + currentSiteNum
                + " out of "
                + clinics.size()
                + " sites. "
                + updatedMap.size()
                + " patient records to be generated.";
        statusMap.put("RSS-message", rssMessage);
        if (renderAllSites == true) {
          log.debug(rssMessage);
        }
        // see if there is already a subscription to this site.
        String importFilePath =
            org.cidrz.webapp.dynasite.Constants.ARCHIVE_PATH
                + siteAbbrev
                + Constants.pathSep
                + "import"
                + Constants.pathSep;
        String importedRssPath = importFilePath + "rss.xml";
        // Reader reader = new BufferedReader((new FileReader(importedRssPath)));
        File importedRssFile = new File(importedRssPath);
        if (importedRssFile.exists() && importedRssFile.length() > 0) {
          final WireFeedInput input = new WireFeedInput();
          WireFeed rssFeed = null;
          XmlReader reader = null;
          Channel channel = null;
          reader = new XmlReader(importedRssFile);
          try {
            rssFeed = input.build(reader);
            rssFeed.setFeedType("rss_2.0");
            channel = (Channel) rssFeed;
            List items = channel.getItems();
            // siteAbbrev = channel.getTitle();
            // Create a list of patients the remote site is "interested" in.
            // ArrayList<String> subscribedPatientList = new ArrayList<String>();
            ArrayList<Patient> subscribedPatientList = new ArrayList<Patient>();
            for (int i = 0; i < items.size(); i++) {
              Item item = (Item) items.get(i);
              // String link = item.getLink();
              String fileName = item.getTitle();
              String zeprsId = fileName.replace(".xml", "");
              try {
                Patient rssPatient = PatientDAO.getOneFromZeprsId(conn, zeprsId);
                // check if the patient is not already in the patientRssList
                if (patientRssMap.get(zeprsId) == null) {
                  subscribedPatientList.add(rssPatient);
                }
              } catch (ObjectNotFoundException e) {
                // Patient may not yet be in system. It should be OK.
              }
            }
            patientRssList.addAll(subscribedPatientList);
          } catch (ParsingFeedException e1) {
            log.debug("Unable to parse RSS feed at " + importedRssPath);
          }
          reader.close();
        }
        // while (patientListResultSet.next()) {
        for (Patient rssPatient : patientRssList) {
          Long patientId = rssPatient.getId();
          String zeprsId = rssPatient.getDistrictPatientid();
          String surname = rssPatient.getSurname();
          Timestamp lastModified = rssPatient.getLastModified();
          Long clinicId =
              rssPatient
                  .getSiteId(); // clinic the patient belongs to - should be the same as the site
                                // feed.
          // get the siteAbbrev from the parent's record only
          // no longer need this part.
          /*try {
          	site = (Site) DynaSiteObjects.getClinicMap().get(clinicId);
          	siteAbbrev = site.getAbbreviation();
          } catch (Exception e) {
          	log.error("Incorrect site data for patient: " + zeprsId + " clinicId: " + clinicId);
          }*/
          String filePath =
              org.cidrz.webapp.dynasite.Constants.ARCHIVE_PATH
                  + siteAbbrev
                  + Constants.pathSep
                  + "local"
                  + Constants.pathSep;
          String fileName = getPatientFilename(zeprsId, surname, patientId);
          // write to the index
          // String indexValue = zeprsId + ":" + siteAbbrev + Constants.pathSep + "local" +
          // Constants.pathSep + fileName + "\n";
          // indexBuffer.append(indexValue);
          indexMap.put(
              zeprsId, siteAbbrev + Constants.pathSep + "local" + Constants.pathSep + fileName);
          // check if the xml file exists. This is useful when testing - you may not have all of the
          // xml files generated yet.
          boolean forceXml = false;
          if (checkXml != null) {
            File filecheck = new File(filePath + fileName);
            if (filecheck.length() == 0) {
              log.debug("Re-rendering file for : " + fileName + " in " + siteAbbrev + "/local.");
              forceXml = true;
            }
          }
          if (updatedMap.get(patientId) != null || forceXml) {
            PatientRecordUtils.archivePatient(conn, patientId, siteAbbrev, filePath, fileName);
            archivedPatientsThisSite++;
            archivedPatientsAllSites++;
            rssMessage =
                "Generating RSS and Patient Xml for "
                    + siteAbbrev
                    + ": "
                    + currentSiteNum
                    + " out of "
                    + clinics.size()
                    + " sites. "
                    + archivedPatientsAllSites
                    + " out of "
                    + updatedMap.size()
                    + " patient records have been generated. "
                    + "Current file: "
                    + filePath
                    + fileName;
            statusMap.put("RSS-message", rssMessage);
            // don't remove from the list because this patient may also be at another site
            // (referral).
            // updatedMap.remove(patientId);
          }
          if (lastModified == null) {
            lastModified = DateUtils.generateTimestamp();
            log.debug("Null lastModified for patient: " + zeprsId);
          }
          java.util.Date pubDate = new Date(lastModified.getTime());
          Item item;
          Description description;
          String url =
              "http://" + ipAddress + ":" + port + "/archive/" + siteAbbrev + "/local/" + fileName;
          item = new Item();
          item.setTitle(fileName);
          item.setLink(url);
          item.setPubDate(pubDate);
          item.setAuthor(clinicId.toString());
          Guid gd = new Guid();
          gd.setPermaLink(true);
          gd.setValue(url);
          item.setGuid(gd);
          description = new Description();
          description.setType("text/plain");
          description.setValue(clinicId.toString());
          item.setDescription(description);
          entries.add(item);
        }
        // Open the remote site RSS file and add patients who may have been transferred
        // ArrayList<String> remotePatientIdList = populateRemotePatientIdList(urlString);
        // log.debug("RSS feed being generated for " + siteAbbrev + " has " + entries.size() + "
        // patients. URL: " + rssFileName);
        sbuf.append(entries.size() + " patients added to " + siteAbbrev + "<br/>");
        feed.setItems(entries);
        Writer writer = new FileWriter(rssFileName);
        WireFeedOutput output = new WireFeedOutput();
        output.output(feed, writer);
        writer.close();
        entries.clear();
      }
      // create the archive listing
      /*Writer indexWriter = new FileWriter(Constants.MASTER_ARCHIVE_INDEX);
      indexWriter.write(indexBuffer.toString());
      indexWriter.flush();
      indexWriter.close();*/
      XmlUtils.save(indexMap, Constants.MASTER_ARCHIVE_INDEX_XML);
      // log.debug("writing index file to " + Constants.MASTER_ARCHIVE_INDEX_JS);
      // XmlUtils.saveJson(indexMap, Constants.MASTER_ARCHIVE_INDEX_JS);
      updatedMap.clear();
      indexMap.clear();
      String rssMessage = "Completed RSS and patient XML generation task";
      statusMap.put("RSS-message", rssMessage);
    }

    // Create the archive
    // Compress.zipDirectory(Constants.ARCHIVE_PATH, Constants.MASTER_ARCHIVE_ZIP);
    // Zip.zip(Constants.ARCHIVE_PATH, Constants.MASTER_ARCHIVE_ZIP,
    // Constants.MASTER_ARCHIVE_CHECKSUM);
    // Zip.CreateZipFile(new File(Constants.MASTER_ARCHIVE_ZIP), new File[]{new
    // File(Constants.ARCHIVE_PATH)}, false);
    message = sbuf.toString();
    return message;
  }