@Override @Transactional(readOnly = false) public Collection updateAndGetRunningCollectionStatusByUser(Long userId) throws Exception { Collection collection = collectionRepository.getRunningCollectionStatusByUser(userId); if (collection != null) { logger.info( "User with ID: '" + userId + "' has a running collection with code: '" + collection.getCode()); return statusByCollection(collection, userId); } else { // logger.info("User with ID: '" + userId + "' don't have any running collections. Nothing to // update." ); // If there is no running collection there is still can be collection with status // 'Initializing'. // This happens because we update collection information from fetcher before // collection was started. // So we need to update from Fetcher this kind of collections as well. collection = collectionRepository.getInitializingCollectionStatusByUser(userId); if (collection != null) { return statusByCollection(collection, userId); } } return null; }
@Override @Transactional public Collection create(CollectionDetailsInfo collectionDetailsInfo, UserAccount user) throws Exception { String filteredTrack = collectionDetailsInfo.getTrack(); if (!StringUtils.isEmpty(filteredTrack)) { filteredTrack = getFilteredTrack(filteredTrack); if (StringUtils.isEmpty(filteredTrack)) { return null; } } Collection collection = adaptCollectionDetailsInfoToCollection(collectionDetailsInfo, user); collection.setTrack(filteredTrack); collection.setUsageType(UsageType.Production); try { collectionRepository.save(collection); collaboratorService.addCollaboratorToCollection( collectionDetailsInfo.getCode(), user.getId()); return collection; } catch (Exception e) { logger.error("Error in creating collection.", e); return null; } }
@Override @Transactional(readOnly = false) public Collection start(Long collectionId) throws Exception { // We are going to start new collection. Lets stop collection which is running for owner of the // new collection. Collection dbCollection = collectionRepository.findById(collectionId); Long userId = dbCollection.getOwner().getId(); Collection alreadyRunningCollection = collectionRepository.getRunningCollectionStatusByUser(userId); if (alreadyRunningCollection != null) { this.stop(alreadyRunningCollection.getId(), userId); } return startFetcher(prepareFetcherRequest(dbCollection), dbCollection); }
@Override @Transactional(readOnly = true) public List<Collection> getStoppedCollections( Integer start, Integer limit, String terms, String sortColumn, String sortDirection) throws Exception { return collectionRepository.getStoppedCollections( start, limit, terms, sortColumn, sortDirection); }
@Override @Transactional(readOnly = true) public Collection findTrashedByCode(String code) throws Exception { Collection temp = collectionRepository.findByCode(code); if (temp.getStatus().equals(CollectionStatus.TRASHED)) { return temp; } return null; }
// MEGHNA: method for stopping a collection on FATAL_ERROR // separate method from stop needed to prevent looping in // updateStatusCollection() method public Collection stopFatalError(Long collectionId, Long userId) throws Exception { Collection collection = collectionRepository.findById(collectionId); // collection = collectionRepository.stop(collection.getId()); Collection updateCollection = stopAidrFetcher(collection, userId); CollectionLog collectionLog = new CollectionLog(collection); collectionLogRepository.save(collectionLog); return updateCollection; }
@Override public List<CollectionSummaryInfo> getAllCollectionDataByUsage(UsageType usage) { /*List<CollectionSummaryInfo> collectionSummaryInfos = new ArrayList<CollectionSummaryInfo>(); List<Collection> collections = collectionRepository.getAllCollectionsByUsage(usage); if(collections != null) { collectionSummaryInfos = adaptCollectionListToCollectionSummaryInfoList(collections); }*/ List<CollectionSummaryInfo> collectionSummaryInfos = collectionRepository.getAllCollectionForAidrData(); return collectionSummaryInfos; }
@Override public List<CollectionBriefInfo> getMicromappersFilteredCollections(boolean micromappersEnabled) { List<Collection> collections = collectionRepository.findMicromappersFilteredCollections(micromappersEnabled); List<CollectionBriefInfo> briefInfos = new ArrayList<CollectionBriefInfo>(); if (collections != null && collections.size() > 0) { briefInfos = adaptCollectionListToCollectionBriefInfoList(collections); } return briefInfos; }
@Override @Transactional(readOnly = false) public Collection startFetcher(FetcherRequestDTO fetcherRequest, Collection collection) { try { /** Rest call to Fetcher */ Client client = ClientBuilder.newBuilder().register(JacksonFeature.class).build(); if (CollectionType.Twitter.equals(collection.getProvider()) || CollectionType.Facebook.equals(collection.getProvider())) { WebTarget webResource = client.target( fetchMainUrl + "/" + collection.getProvider().toString().toLowerCase() + "/start"); ObjectMapper objectMapper = JacksonWrapper.getObjectMapper(); Response clientResponse = webResource .request(MediaType.APPLICATION_JSON) .post(Entity.json(objectMapper.writeValueAsString(fetcherRequest)), Response.class); // logger.info("ObjectMapper: " + objectMapper.writeValueAsString(fetcherRequest)); String jsonString = clientResponse.readEntity(String.class); JSONParser parser = new JSONParser(); JSONObject jsonResponse = (JSONObject) parser.parse(jsonString); // logger.info("NEW STRING: " + jsonResponse); FetcheResponseDTO response = objectMapper.readValue(jsonResponse.get("entity").toString(), FetcheResponseDTO.class); logger.info("start Response from fetchMain " + objectMapper.writeValueAsString(response)); collection.setStatus(CollectionStatus.getByStatus(response.getStatusCode())); } else if (CollectionType.SMS.equals(collection.getProvider())) { WebTarget webResource = client.target( fetchMainUrl + "/sms/start?collection_code=" + URLEncoder.encode(collection.getCode(), "UTF-8")); Response response = webResource.request(MediaType.APPLICATION_JSON).get(); if (response.getStatus() == 200) collection.setStatus(CollectionStatus.INITIALIZING); } /** Update Status To database */ collectionRepository.update(collection); return collection; } catch (Exception e) { logger.error("Error while starting Remote FetchMain Collection", e); } return null; }
@Override @Transactional(readOnly = false) public Collection stop(Long collectionId, Long userId) throws Exception { Collection collection = collectionRepository.findById(collectionId); // Follwoing 2 lines added by koushik for downloadCount bug Collection c = this.statusByCollection(collection, userId); collection.setCount(c.getCount()); Collection updateCollection = stopAidrFetcher(collection, userId); CollectionLog collectionLog = new CollectionLog(collection); collectionLog.setUpdatedBy(userId); collectionLogRepository.save(collectionLog); return updateCollection; }
@Override @Transactional(readOnly = true) public Integer getPublicCollectionsCount(Enum statusValue) throws Exception { return collectionRepository.getPublicCollectionsCount(statusValue); }
@Override @Transactional(readOnly = true) public Long getStoppedCollectionsCount(String terms) throws Exception { return collectionRepository.getStoppedCollectionsCount(terms); }
@Override @Transactional(readOnly = true) public Integer getCollectionsCount(UserAccount user, boolean onlyTrashed) throws Exception { return collectionRepository.getCollectionsCount(user, onlyTrashed); }
@Override @Transactional(readOnly = true) public Long getTotalCollectionsCount() { return collectionRepository.getTotalCollectionsCount(); }
@Override public List<Collection> getUnexpectedlyStoppedCollections(Date today) { return collectionRepository.getUnexpectedlyStoppedCollections(today); }
@Override @Transactional(readOnly = true) public Boolean existName(String name) throws Exception { return collectionRepository.existName(name); }
@Override public Long getRunningCollectionsCount() { return collectionRepository.getRunningCollectionsCount(""); }
@Override @Transactional(readOnly = true) public List<Collection> findAll( Integer start, Integer limit, UserAccount user, boolean onlyTrashed) throws Exception { return collectionRepository.getPaginatedData(start, limit, user, onlyTrashed); }
private Collection updateStatusCollection( String jsonResponse, Collection collection, Long accountId) throws Exception { ObjectMapper objectMapper = JacksonWrapper.getObjectMapper(); FetcheResponseDTO response = objectMapper.readValue(jsonResponse, FetcheResponseDTO.class); if (response != null) { // MEGHNA: moved setting collection count to top of the method // to avoid individual status blocks setting collection count below if (response.getCollectionCount() != null && !response.getCollectionCount().equals(collection.getCount())) { collection.setCount(response.getCollectionCount()); String lastDocument = response.getLastDocument(); collection.setLastExecutionTime(response.getLastExecutionTime()); if (lastDocument != null) collection.setLastDocument(lastDocument); collectionRepository.update(collection); } collection.setSourceOutage(response.isSourceOutage()); if (!CollectionStatus.getByStatus(response.getStatusCode()).equals(collection.getStatus())) { CollectionStatus prevStatus = collection.getStatus(); collection.setStatus(CollectionStatus.getByStatus(response.getStatusCode())); switch (CollectionStatus.getByStatus(response.getStatusCode())) { case NOT_FOUND: // case STOPPED: collection.setStatus(CollectionStatus.NOT_RUNNING); // Add collectionCount in collectionLog if it was not recorded. if (collection.getStartDate() != null && ((collection.getEndDate() != null && collection.getStartDate().after(collection.getEndDate())) || collection.getEndDate() == null)) { if (collectionLogRepository.countLogsStartedInInterval( collection.getId(), collection.getStartDate(), new Date()) == 0) { CollectionLog collectionLog = new CollectionLog(collection); collectionLog.setEndDate(new Date()); collectionLog.setUpdatedBy(accountId); collectionLogRepository.save(collectionLog); } } case RUNNING_WARNING: if (prevStatus == CollectionStatus.INITIALIZING) { collection = collectionRepository.start(collection.getId()); break; } case WARNING: collectionRepository.update(collection); break; case RUNNING: collection = collectionRepository.start(collection.getId()); break; case FATAL_ERROR: // collection = collectionRepository.stop(collection.getId()); logger.warn("Fatal error, stopping collection " + collection.getId()); if (prevStatus != CollectionStatus.FATAL_ERROR || prevStatus != CollectionStatus.NOT_RUNNING || prevStatus != CollectionStatus.STOPPED) this.stopFatalError(collection.getId(), accountId); break; case EXCEPTION: logger.warn( "Rejected Thread Execution Exception, restarting collection " + collection.getId()); if (prevStatus != CollectionStatus.EXCEPTION) { this.stopFatalError(collection.getId(), accountId); this.start(collection.getId()); } break; default: break; } } } return collection; }
@Override @Transactional(readOnly = false) public boolean updateCollection(CollectionUpdateInfo collectionUpdateInfo, Long userId) { String filteredTrack = ""; try { Collection collection = findByCode(collectionUpdateInfo.getCode()); if (!collection.getName().equals(collectionUpdateInfo.getName())) { if (!existName(collectionUpdateInfo.getName())) { collection.setName(collectionUpdateInfo.getName()); } else { return false; } } // if collection exists with same name collection.setProvider(CollectionType.valueOf(collectionUpdateInfo.getProvider())); collection.setFollow(collectionUpdateInfo.getFollow()); collection.setFetchInterval(collectionUpdateInfo.getFetchInterval()); collection.setFetchFrom(collectionUpdateInfo.getFetchFrom()); filteredTrack = collectionUpdateInfo.getTrack(); if (!StringUtils.isEmpty(filteredTrack)) { filteredTrack = getFilteredTrack(filteredTrack); if (StringUtils.isEmpty(filteredTrack)) { return false; } } collection.setTrack(filteredTrack); collection.setGeo(collectionUpdateInfo.getGeo()); collection.setGeoR(collectionUpdateInfo.getGeoR()); collection.setDurationHours(Integer.parseInt(collectionUpdateInfo.getDurationHours())); collection.setLangFilters(collectionUpdateInfo.getLangFilters()); Long crisisTypeId = Long.parseLong(collectionUpdateInfo.getCrisisType()); CrisisType crisisType = crisisTypeService.getById(crisisTypeId); if (crisisType != null) { collection.setCrisisType(crisisType); } else { logger.error( "Crisis Type Id: " + crisisTypeId + " does not exist. Can't update the collection : " + collectionUpdateInfo.getCode()); return false; } if (CollectionType.SMS.equals(collection.getProvider())) { collection.setTrack(null); collection.setLangFilters(null); collection.setGeo(null); collection.setGeoR(null); collection.setFollow(null); } if (collection.getProvider() == CollectionType.Twitter) { collection.setFollow( this.getFollowTwitterIDs( collectionUpdateInfo.getFollow(), collection.getOwner().getUserName())); } collectionRepository.update(collection); // first make an entry in log if collection is running if (CollectionStatus.RUNNING_WARNING.equals(collection.getStatus()) || CollectionStatus.RUNNING.equals(collection.getStatus())) { this.stop(collection.getId(), userId); this.startFetcher(this.prepareFetcherRequest(collection), collection); } return true; } catch (Exception e) { logger.error("Unable to update the collection : " + collectionUpdateInfo.getCode(), e); return false; } }
@Override @Transactional(readOnly = false) public void delete(Collection collection) throws Exception { collectionRepository.delete(collection); }
@Override @Transactional(readOnly = true) public List<Collection> findAllForPublic(Integer start, Integer limit, Enum statusValue) throws Exception { return collectionRepository.getPaginatedDataForPublic(start, limit, statusValue); }
// this method is common to get collection and should not filter by status @Override @Transactional(readOnly = true) public Collection findById(Long id) throws Exception { return collectionRepository.findById(id); }
@Override @Transactional(readOnly = true) public Collection getRunningCollectionStatusByUser(Long userId) throws Exception { return collectionRepository.getRunningCollectionStatusByUser(userId); }
@Override @Transactional(readOnly = true) public List<Collection> geAllCollectionByUser(Long userId) throws Exception { return collectionRepository.getAllCollectionByUser(userId); }
@Override @Transactional(readOnly = true) public List<Collection> getRunningCollections() throws Exception { return collectionRepository.getRunningCollections(); }
// this method is common to get collection and should not filter by status @Override @Transactional(readOnly = true) public Collection findByCode(String code) throws Exception { return collectionRepository.findByCode(code); }
@Override @Transactional(readOnly = true) public List<Collection> searchByName(String query, Long userId) throws Exception { return collectionRepository.searchByName(query, userId); }
@Override public List<String> fetchEligibleFacebookCollectionsToReRun() { List<String> codes = collectionRepository.getEligibleFacebookCollectionsToReRun(); return codes; }
@Override @Transactional(readOnly = false) public void update(Collection collection) { collectionRepository.update(collection); }