@Override public void addFields(Document bannerDoc, AdDefinition bannerDefinition) { KeywordConditionDefinition kdef = null; if (bannerDefinition.hasConditionDefinition(ConditionDefinitions.KEYWORD)) { kdef = (KeywordConditionDefinition) bannerDefinition.getConditionDefinition(ConditionDefinitions.KEYWORD); } if (kdef != null && kdef.getKeywords().size() > 0) { // keywords im Dokument speichern List<Keyword> kws = kdef.getKeywords(); for (Keyword k : kws) { bannerDoc.add(new StringField(AdDBConstants.ADDB_AD_KEYWORD, k.word, Field.Store.NO)); } } else { /* * für alle Banner ohne angegebenem Keyword wird das default ALL-Keyword gesetzt */ bannerDoc.add( new StringField( AdDBConstants.ADDB_AD_KEYWORD, AdDBConstants.ADDB_AD_KEYWORD_ALL, Field.Store.NO)); } }
@Test public void test1_KeyValueCondition() throws Exception { System.out.println("test1_KeyValueCondition"); manager.getContext().validKeys.clear(); manager.getContext().validKeys.add("browser"); db.open(); AdDefinition b = new ImageAdDefinition(); b.setId("1"); KeyValueConditionDefinition sdef = new KeyValueConditionDefinition(); sdef.getKeyValues().add(new KeyValue("browser", "firefox")); sdef.getKeyValues().add(new KeyValue("browser", "chrome")); b.addConditionDefinition(ConditionDefinitions.KEYVALUE, sdef); b.setFormat(new FullBannerAdFormat()); db.addBanner(b); b = new ImageAdDefinition(); b.setId("2"); sdef = new KeyValueConditionDefinition(); sdef.getKeyValues().add(new KeyValue("browser", "firefox")); sdef.getKeyValues().add(new KeyValue("browser", "ie")); b.addConditionDefinition(ConditionDefinitions.KEYVALUE, sdef); b.setFormat(new FullBannerAdFormat()); db.addBanner(b); db.reopen(); AdRequest request = new AdRequest(); List<AdFormat> formats = new ArrayList<AdFormat>(); formats.add(new FullBannerAdFormat()); request.formats(formats); List<AdType> types = new ArrayList<AdType>(); types.add(AdTypes.forType(ImageAdType.TYPE)); request.types(types); request.keyValues().put("browser", "opera"); List<AdDefinition> result = db.search(request); assertTrue(result.isEmpty()); request.keyValues().clear(); request.keyValues().put("browser", "firefox"); result = db.search(request); assertEquals(2, result.size()); request.keyValues().clear(); request.keyValues().put("browser", "chrome"); result = db.search(request); assertEquals(1, result.size()); assertTrue(result.get(0).getId().equals("1")); request.keyValues().clear(); request.keyValues().put("browser", "ie"); result = db.search(request); assertEquals(1, result.size()); assertTrue(result.get(0).getId().equals("2")); }
@Override public void add(AdDefinition definition) throws IOException { // add index Document doc = LuceneDocumentHelper.getInstance().getBannerDocument(definition, this.addb); this.writer.addDocument(doc, new KeywordAnalyzer()); // add to map this.store.put(definition.getId(), definition); if (!memoryMode) { this.db.commit(); } }
@Override public void addFields(BasicDBObject bannerDoc, AdDefinition bannerDefinition) { DateConditionDefinition ddef = null; if (bannerDefinition.hasConditionDefinition(ConditionDefinitions.DATE)) { ddef = (DateConditionDefinition) bannerDefinition.getConditionDefinition(ConditionDefinitions.DATE); } if (ddef != null && !ddef.getPeriods().isEmpty()) { /* * Um die Paare von/zu der Perioden zu erhalten, werden die jeweilige Felder geprefixt. * Dadurch können bei der Suche die einzelnen Perioden unterschieden werden */ int count = 0; for (Period p : ddef.getPeriods()) { if (p.getFrom() != null) { bannerDoc.put(AdDBConstants.ADDB_AD_DATE_FROM + count, p.getFrom()); } else { bannerDoc.put(AdDBConstants.ADDB_AD_DATE_FROM + count, AdDBConstants.ADDB_AD_DATE_ALL); } if (p.getTo() != null) { bannerDoc.put(AdDBConstants.ADDB_AD_DATE_TO + count, p.getTo()); } else { bannerDoc.put(AdDBConstants.ADDB_AD_DATE_TO + count, AdDBConstants.ADDB_AD_DATE_ALL); } count++; } // for (; count < DateConditionDefinition.MAX_PERIOD_COUNT; count++) { // bannerDoc.put(AdDBConstants.ADDB_AD_DATE_FROM + count, AdDBConstants.ADDB_AD_DATE_ALL); // bannerDoc.put(AdDBConstants.ADDB_AD_DATE_TO + count, AdDBConstants.ADDB_AD_DATE_ALL); // } } else { bannerDoc.put(AdDBConstants.ADDB_AD_DATE_FROM + 0, AdDBConstants.ADDB_AD_DATE_ALL); bannerDoc.put(AdDBConstants.ADDB_AD_DATE_TO + 0, AdDBConstants.ADDB_AD_DATE_ALL); } }
@Override public void addFields(BasicDBObject bannerDoc, AdDefinition bannerDefinition) { DayConditionDefinition ddef = null; if (bannerDefinition.hasConditionDefinition(ConditionDefinitions.DAY)) { ddef = (DayConditionDefinition) bannerDefinition.getConditionDefinition(ConditionDefinitions.DAY); } if (ddef != null && ddef.getDays().size() > 0) { List<Day> list = ddef.getDays(); List<String> days = new ArrayList<String>(); for (Day day : list) { days.add(String.valueOf(day.getDay())); } bannerDoc.put(AdDBConstants.ADDB_AD_DAY, days); } else { List<String> days = new ArrayList<String>(); days.add(AdDBConstants.ADDB_AD_DAY_ALL); bannerDoc.put(AdDBConstants.ADDB_AD_DAY, days); } }