<commit_before>/*
   Copyright 2017 Ericsson AB.
   For a full list of individual contributors, please see the commit history.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package com.ericsson.ei.waitlist;

import java.util.ArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.ericsson.ei.handlers.MatchIdRulesHandler;
import com.ericsson.ei.jmespath.JmesPathInterface;
import com.ericsson.ei.mongodbhandler.MongoDBHandler;
import com.ericsson.ei.rmqhandler.RmqHandler;
import com.ericsson.ei.rules.RulesHandler;
import com.ericsson.ei.rules.RulesObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.mongodb.DBObject;
import com.mongodb.util.JSON;

@Component
public class WaitListWorker {

    @Autowired
    private WaitListStorageHandler waitListStorageHandler;

    @Autowired
    private RmqHandler rmqHandler;

    @Autowired
    private RulesHandler rulesHandler;

    @Autowired
    private JmesPathInterface jmesPathInterface;

    @Autowired
    private MatchIdRulesHandler matchIdRulesHandler;

    static Logger log = (Logger) LoggerFactory.getLogger(WaitListWorker.class);

    @Scheduled(initialDelayString = "${waitlist.initialDelayResend}", fixedRateString = "${waitlist.fixedRateResend}")
    public void run() {
        RulesObject rulesObject = null;
        ArrayList<String> documents = waitListStorageHandler.getWaitList();
        for (String document : documents) {
            DBObject dbObject = (DBObject) JSON.parse(document);
            String event = dbObject.get("Event").toString();
            rulesObject = rulesHandler.getRulesForEvent(event);
            String idRule = rulesObject.getIdentifyRules();
            JsonNode ids = jmesPathInterface.runRuleOnEvent(idRule, event);
            if (ids.isArray()) {
                for (final JsonNode idJsonObj : ids) {
                    ArrayList<String> objects = matchIdRulesHandler.fetchObjectsById(rulesObject, idJsonObj.textValue());
                    if (objects.size() > 0) {
                        rmqHandler.publishObjectToWaitlistQueue(event);
                        waitListStorageHandler.dropDocumentFromWaitList(document);
                    }
                }
            }
        }
    }
}<commit_msg>avoid empty id rules<commit_after>/*
   Copyright 2017 Ericsson AB.
   For a full list of individual contributors, please see the commit history.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package com.ericsson.ei.waitlist;

import java.util.ArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.ericsson.ei.handlers.MatchIdRulesHandler;
import com.ericsson.ei.jmespath.JmesPathInterface;
import com.ericsson.ei.mongodbhandler.MongoDBHandler;
import com.ericsson.ei.rmqhandler.RmqHandler;
import com.ericsson.ei.rules.RulesHandler;
import com.ericsson.ei.rules.RulesObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.mongodb.DBObject;
import com.mongodb.util.JSON;

@Component
public class WaitListWorker {

    @Autowired
    private WaitListStorageHandler waitListStorageHandler;

    @Autowired
    private RmqHandler rmqHandler;

    @Autowired
    private RulesHandler rulesHandler;

    @Autowired
    private JmesPathInterface jmesPathInterface;

    @Autowired
    private MatchIdRulesHandler matchIdRulesHandler;

    static Logger log = (Logger) LoggerFactory.getLogger(WaitListWorker.class);

    @Scheduled(initialDelayString = "${waitlist.initialDelayResend}", fixedRateString = "${waitlist.fixedRateResend}")
    public void run() {
        RulesObject rulesObject = null;
        ArrayList<String> documents = waitListStorageHandler.getWaitList();
        for (String document : documents) {
            DBObject dbObject = (DBObject) JSON.parse(document);
            String event = dbObject.get("Event").toString();
            rulesObject = rulesHandler.getRulesForEvent(event);
            String idRule = rulesObject.getIdentifyRules();
            if (idRule != null && !idRule.isEmpty()) {
                JsonNode ids = jmesPathInterface.runRuleOnEvent(idRule, event);
                if (ids.isArray()) {
                    for (final JsonNode idJsonObj : ids) {
                        ArrayList<String> objects = matchIdRulesHandler.fetchObjectsById(rulesObject, idJsonObj.textValue());
                        if (objects.size() > 0) {
                            rmqHandler.publishObjectToWaitlistQueue(event);
                            waitListStorageHandler.dropDocumentFromWaitList(document);
                        }
                    }
                }
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.08030154047853164, 'fraction_numerical': 0.003605375286791216, 'mean_word_length': 3.2002752924982794, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7148225', 'n_tokens_mistral': 1629, 'n_tokens_neox': 1581, 'n_words': 421}