Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5247970dbe7cd40599567fbd9f86fd1e85724b63 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/*******************************************************************************
 * Copyright (c) 2016 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.define.report.internal.wordupdate;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Stack;
import java.util.TreeMap;
import java.util.regex.Matcher;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.codehaus.jackson.map.ObjectMapper;
import org.eclipse.osee.framework.core.data.ArtifactId;
import org.eclipse.osee.framework.core.data.BranchId;
import org.eclipse.osee.framework.core.data.BranchViewData;
import org.eclipse.osee.framework.core.data.FeatureDefinitionData;
import org.eclipse.osee.framework.core.enums.BranchType;
import org.eclipse.osee.framework.core.enums.CoreArtifactTypes;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.core.grammar.ApplicabilityBlock;
import org.eclipse.osee.framework.core.grammar.ApplicabilityBlock.ApplicabilityType;
import org.eclipse.osee.framework.core.grammar.ApplicabilityGrammarLexer;
import org.eclipse.osee.framework.core.grammar.ApplicabilityGrammarParser;
import org.eclipse.osee.framework.core.util.WordCoreUtil;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.ResultSet;
import org.eclipse.osee.logger.Log;
import org.eclipse.osee.orcs.OrcsApi;
import org.eclipse.osee.orcs.data.ArtifactReadable;
import org.eclipse.osee.orcs.data.BranchReadable;

public class WordMLApplicabilityHandler {

   private static String SCRIPT_ENGINE_NAME = "JavaScript";

   private Set<String> validConfigurations;
   private Map<String, List<String>> viewApplicabilitiesMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
   private final String configurationToView;
   private final Stack<ApplicabilityBlock> applicBlocks;
   private final String featureDefinitionJson;
   private final ScriptEngine se;
   private final Log logger;

   public WordMLApplicabilityHandler(OrcsApi orcsApi, Log logger, BranchId branch, ArtifactId view) {
      this.applicBlocks = new Stack<>();
      this.logger = logger;

      ScriptEngineManager sem = new ScriptEngineManager();
      se = sem.getEngineByName(SCRIPT_ENGINE_NAME);

      BranchId branchToUse = getBranchToUse(orcsApi, branch, view);

      validConfigurations = getValidConfigurations(orcsApi, branch);

      viewApplicabilitiesMap =
         orcsApi.getQueryFactory().applicabilityQuery().getBranchViewFeatureValues(branchToUse, view);
      configurationToView = viewApplicabilitiesMap.get("config").iterator().next();

      BranchReadable br = orcsApi.getQueryFactory().branchQuery().andId(branch).getResults().getOneOrNull();
      if (br.getBranchType().equals(BranchType.MERGE)) {
         branch = br.getParentBranch();
      }

      ArtifactReadable featureDefArt = orcsApi.getQueryFactory().fromBranch(branch).andTypeEquals(
         CoreArtifactTypes.FeatureDefinition).getResults().getExactlyOne();
      featureDefinitionJson = featureDefArt.getSoleAttributeAsString(CoreAttributeTypes.GeneralStringData);
   }

   public String previewValidApplicabilityContent(String content) throws OseeCoreException {
      String toReturn = content;
      int searchIndex = 0;
      int applicBlockCount = 0;

      Matcher matcher = WordCoreUtil.FULL_PATTERN.matcher(toReturn);

      while (searchIndex < toReturn.length() && matcher.find(searchIndex)) {
         String beginFeature = matcher.group(1) != null ? matcher.group(1) : null;
         String beginConfig = matcher.group(26) != null ? matcher.group(26) : null;

         String endFeature = matcher.group(12) != null ? WordCoreUtil.textOnly(matcher.group(12)).toLowerCase() : null;
         String endConfig = matcher.group(43) != null ? WordCoreUtil.textOnly(matcher.group(43)).toLowerCase() : null;

         if (beginFeature != null && WordCoreUtil.textOnly(beginFeature).toLowerCase().contains(
            WordCoreUtil.FEATUREAPP)) {
            applicBlockCount += 1;
            searchIndex =
               addApplicabilityBlock(ApplicabilityType.Feature, matcher, beginFeature, searchIndex, toReturn);

         } else if (beginConfig != null && WordCoreUtil.textOnly(beginConfig).toLowerCase().contains(
            WordCoreUtil.CONFIGAPP)) {
            if (isValidConfigurationBracket(beginConfig)) {
               applicBlockCount += 1;
               searchIndex =
                  addApplicabilityBlock(ApplicabilityType.Configuration, matcher, beginConfig, searchIndex, toReturn);
            } else {
               searchIndex = matcher.end();
            }

         } else if ((endFeature != null && endFeature.contains(
            WordCoreUtil.FEATUREAPP)) || (endConfig != null && endConfig.contains(WordCoreUtil.CONFIGAPP))) {

            ApplicabilityBlock applicabilityBlock = getFullApplicabilityBlock(matcher, toReturn);

            if (applicabilityBlock == null) {
               searchIndex = matcher.end();
            } else {
               applicBlockCount -= 1;
               String toInsert = evaluateApplicabilityBlock(applicabilityBlock, toReturn);
               String toReplace =
                  toReturn.substring(applicabilityBlock.getStartInsertIndex(), applicabilityBlock.getEndInsertIndex());
               toReturn = toReturn.replace(toReplace, toInsert);
               searchIndex =
                  applicabilityBlock.getStartInsertIndex() + (applicabilityBlock.isInTable() ? 0 : toInsert.length());
               matcher = WordCoreUtil.FULL_PATTERN.matcher(toReturn);
            }
         } else {
            break;
         }
      }

      toReturn = removeEmptyLists(toReturn);
      if (applicBlockCount != 0) {
         logger.error("An applicability block of text is missing an End Feature/Configuration tag");
      }

      return toReturn;
   }

   private boolean isValidConfigurationBracket(String beginConfig) {
      beginConfig = WordCoreUtil.textOnly(beginConfig);
      int start = beginConfig.indexOf("[") + 1;
      int end = beginConfig.indexOf("]");
      String applicExpText = beginConfig.substring(start, end);

      String[] configs = applicExpText.split("&|\\|");
      for (int i = 0; i < configs.length; i++) {
         configs[i] = configs[i].split("=")[0].trim();
         if (!containsIgnoreCase(validConfigurations, configs[i])) {
            return false;
         }
      }

      return true;
   }

   private String removeExtraParagraphs(String fullWordMl, String toInsert, ApplicabilityBlock applicabilityBlock) {
      int startInsertIndex = applicabilityBlock.getStartInsertIndex();

      if (!applicabilityBlock.isInTable() && (toInsert.isEmpty() || toInsert.startsWith(
         WordCoreUtil.WHOLE_END_PARAGRAPH))) {
         String findParagraphStart = fullWordMl.substring(0, startInsertIndex);
         int paragraphStartIndex = findParagraphStart.lastIndexOf(WordCoreUtil.START_PARAGRAPH);

         // check this doesn't contain feature/config tags
         String beginningText = fullWordMl.substring(paragraphStartIndex, startInsertIndex);

         if (toInsert.isEmpty() && paragraphStartIndex >= 0 && !beginningText.matches(
            "(?i).*?(" + WordCoreUtil.BEGINFEATURE + "|" + WordCoreUtil.BEGINCONFIG + "|" + WordCoreUtil.ENDCONFIG + "|" + WordCoreUtil.ENDFEATURE + ").*?")) {
            int endInsertIndex = applicabilityBlock.getEndInsertIndex();
            String findParagraphEnd = fullWordMl.substring(endInsertIndex);

            int paragraphEndIndex = findParagraphEnd.indexOf(WordCoreUtil.END_PARAGRAPH) + endInsertIndex + 6;
            if (paragraphEndIndex >= 0) {
               // check this doesn't contain feature/config tags
               String endText = fullWordMl.substring(endInsertIndex, paragraphEndIndex);

               if (paragraphEndIndex >= 0 && WordCoreUtil.textOnly(endText).isEmpty() && !endText.matches(
                  "(?i).*?(" + WordCoreUtil.BEGINFEATURE + "|" + WordCoreUtil.BEGINCONFIG + "|" + WordCoreUtil.ENDCONFIG + "|" + WordCoreUtil.ENDFEATURE + ").*?")) {
                  applicabilityBlock.setStartInsertIndex(paragraphStartIndex);
                  applicabilityBlock.setStartTextIndex(paragraphStartIndex);
                  applicabilityBlock.setEndInsertIndex(paragraphEndIndex);
                  applicabilityBlock.setEndTextIndex(paragraphEndIndex);
               }
            }
         } else {
            String findParagraphEnd = fullWordMl.substring(startInsertIndex);
            int paragraphEndIndex = findParagraphEnd.indexOf(WordCoreUtil.END_PARAGRAPH) + startInsertIndex + 6;

            if (paragraphStartIndex >= 0 && paragraphEndIndex >= 0 && paragraphEndIndex > paragraphStartIndex) {
               String fullParagraph = fullWordMl.substring(paragraphStartIndex, paragraphEndIndex);
               fullParagraph =
                  fullParagraph.replaceFirst("(?i)" + WordCoreUtil.BEGINFEATURE + "|" + WordCoreUtil.BEGINCONFIG, "");

               if (WordCoreUtil.textOnly(fullParagraph).isEmpty()) {
                  toInsert = toInsert.replaceFirst(WordCoreUtil.WHOLE_END_PARAGRAPH, "");
                  applicabilityBlock.setStartInsertIndex(paragraphStartIndex);
                  applicabilityBlock.setStartTextIndex(paragraphEndIndex);
               }
            }
         }
      }

      if (!applicabilityBlock.isInTable() && toInsert.matches(
         ".*?<w:p wsp:rsid[^>]+><w:pPr><w:spacing w:after=[^>]+></w:spacing></w:pPr><w:r><w:t>$")) {

         int origLength = toInsert.length();
         int lastParaIndex = toInsert.lastIndexOf(WordCoreUtil.START_PARAGRAPH);
         if (lastParaIndex >= 0) {
            toInsert = toInsert.substring(0, lastParaIndex);
            applicabilityBlock.setEndTextIndex(applicabilityBlock.getEndTextIndex() - (origLength - lastParaIndex));
            applicabilityBlock.setEndInsertIndex(
               applicabilityBlock.getEndInsertIndex() + WordCoreUtil.WHOLE_END_PARAGRAPH.length());
         }
      }

      return toInsert;
   }

   // End Bracket can contain multiple feature/value pairs
   private boolean isValidFeatureBracket(String optionalEndBracket) {
      String text = WordCoreUtil.textOnly(optionalEndBracket);
      text = text.replaceAll("\\[", "");
      text = text.replaceAll("\\]", "").trim();

      // Split on ORs and ANDs
      String[] featureValueStrings = text.split("\\||&");
      for (String featureValueString : featureValueStrings) {
         String[] split = featureValueString.split("=");
         String featName = split[0].trim();
         String featVal = split.length > 1 ? split[1].trim() : null;

         if (viewApplicabilitiesMap.containsKey(featName)) {
            List<String> values = viewApplicabilitiesMap.get(featName);
            if (featVal != null && !containsIgnoreCase(values, featVal)) {
               return false;
            }
         } else {
            return false;
         }
      }

      return true;
   }

   private String evaluateApplicabilityBlock(ApplicabilityBlock applicabilityBlock, String fullWordML) {
      Map<String, String> binDataMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
      saveBinData(applicabilityBlock.getFullText(), binDataMap);

      String toInsert = evaluateApplicabilityExpression(applicabilityBlock);
      toInsert = insertMissingbinData(toInsert, binDataMap);
      toInsert = removeExtraParagraphs(fullWordML, toInsert, applicabilityBlock);

      return toInsert;
   }

   private String removeEmptyLists(String wordML) {
      return wordML.replaceAll(WordCoreUtil.EMPTY_LIST_REGEX, "");
   }

   private String insertMissingbinData(String toInsert, Map<String, String> binDataMap) {
      String temp = toInsert;
      Matcher matcher = WordCoreUtil.IMG_SRC_PATTERN.matcher(temp);
      while (matcher.find()) {
         String srcId = matcher.group(1);
         if (binDataMap.containsKey(srcId)) {
            String binData = binDataMap.get(srcId);
            if (!temp.contains(binData)) {
               temp = binData + temp;
            }
         }
      }

      return temp;
   }

   private void saveBinData(String fullText, Map<String, String> binDataMap) {
      Matcher matcher = WordCoreUtil.BIN_DATA_PATTERN.matcher(fullText);
      while (matcher.find()) {
         binDataMap.put(matcher.group(1), matcher.group(0));
      }
   }

   private int addApplicabilityBlock(ApplicabilityType type, Matcher matcher, String applicabilityExpression, int searchIndex, String fullWordMl) {
      ApplicabilityBlock beginApplic = new ApplicabilityBlock();
      beginApplic.setType(type);
      //Remove extra space
      String applicExpText = WordCoreUtil.textOnly(applicabilityExpression).toLowerCase().replace(" [", "[");
      beginApplic.setApplicabilityExpression(applicExpText);
      beginApplic.setStartInsertIndex(matcher.start());
      beginApplic.setStartTextIndex(matcher.end());
      applicBlocks.push(beginApplic);
      searchIndex = matcher.end();

      return searchIndex;
   }

   private ApplicabilityBlock getFullApplicabilityBlock(Matcher matcher, String toReturn) {
      if (applicBlocks.isEmpty()) {
         logger.error("An applicability block of text is missing a start Feature/Configuration tag");
         return null;
      }
      ApplicabilityBlock applic = applicBlocks.pop();

      // set end insert index - Check if End Bracket is valid
      String optionalEndBracket = null;
      boolean isValidBracket = false;
      if (applic.getType().equals(ApplicabilityType.Configuration)) {
         int endBracketGroup = 60;
         optionalEndBracket = matcher.group(endBracketGroup);
         isValidBracket = optionalEndBracket == null ? false : isValidConfigurationBracket(optionalEndBracket);
      } else {
         int endBracketGroup = 23;
         optionalEndBracket = matcher.group(endBracketGroup);
         isValidBracket = optionalEndBracket == null ? false : isValidFeatureBracket(optionalEndBracket);
      }

      if (optionalEndBracket != null && !isValidBracket) {
         int newEndInsertIndex = matcher.end() - optionalEndBracket.length();
         applic.setEndInsertIndex(newEndInsertIndex);
      } else {
         applic.setEndInsertIndex(matcher.end());
      }
      applic.setEndTextIndex(matcher.start());

      String insideText = toReturn.substring(applic.getStartTextIndex(), applic.getEndTextIndex());
      applic.setFullText(insideText);

      // Adjust start and end insert indicies if tags are inside a table
      if (!applic.getFullText().contains(WordCoreUtil.TABLE) && applic.getFullText().contains(
         WordCoreUtil.TABLE_CELL)) {
         String findStartOfRow = toReturn.substring(0, applic.getStartInsertIndex());
         int startRowIndex = findStartOfRow.lastIndexOf(WordCoreUtil.START_TABLE_ROW);

         if (startRowIndex != -1) {
            // find end of row after the END configuration/feature tag
            String findEndOfRow = toReturn.substring(matcher.end());
            int endRowIndex = findEndOfRow.indexOf(WordCoreUtil.END_TABLE_ROW);
            if (endRowIndex != -1) {
               endRowIndex = endRowIndex + matcher.end() + 7;
               String fullText = toReturn.substring(startRowIndex, endRowIndex);
               applic.setIsInTable(true);
               applic.setStartInsertIndex(startRowIndex);
               applic.setEndInsertIndex(startRowIndex + fullText.length());

               fullText =
                  fullText.replaceFirst("(?i)(" + WordCoreUtil.ENDFEATURE + "|" + WordCoreUtil.ENDCONFIG + ")", "");
               fullText =
                  fullText.replaceFirst("(?i)(" + WordCoreUtil.BEGINFEATURE + "|" + WordCoreUtil.BEGINCONFIG + ")", "");
               applic.setFullText(fullText);
            }
         }
      }

      return applic;
   }

   private static BranchId getBranchToUse(OrcsApi orcsApi, BranchId branch, ArtifactId viewId) {
      BranchId branchView = findBranchView(orcsApi, viewId);
      return branchView == null ? branch : branchView;
   }

   private static BranchId findBranchView(OrcsApi orcsApi, ArtifactId viewId) {
      BranchId branchToUse = null;
      boolean foundBranchView = false;
      List<BranchViewData> views = orcsApi.getQueryFactory().applicabilityQuery().getViews();
      for (BranchViewData viewData : views) {
         List<ArtifactId> branchViews = viewData.getBranchViews();
         for (ArtifactId id : branchViews) {
            if (viewId.equals(id)) {
               branchToUse = viewData.getBranch();
               foundBranchView = true;
               break;
            }
         }
         if (foundBranchView) {
            break;
         }
      }
      return branchToUse;
   }

   private String evaluateApplicabilityExpression(ApplicabilityBlock applic) {
      String applicabilityExpression = applic.getApplicabilityExpression();
      String toInsert = "";
      try {

         String fullText = applic.getFullText();

         ApplicabilityGrammarLexer lex =
            new ApplicabilityGrammarLexer(new ANTLRStringStream(applicabilityExpression.toUpperCase()));
         ApplicabilityGrammarParser parser = new ApplicabilityGrammarParser(new CommonTokenStream(lex));

         parser.start();

         if (applic.getType().equals(ApplicabilityType.Feature)) {
            toInsert =
               getValidFeatureContent(fullText, applic.isInTable(), parser.getIdValuesMap(), parser.getOperators());
         } else if (applic.getType().equals(ApplicabilityType.Configuration)) {
            toInsert = getValidConfigurationContent(fullText, parser.getIdValuesMap());
         }

      } catch (RecognitionException ex) {
         logger.error(
            "Failed to parse expression: " + applicabilityExpression + " at start Index: " + applic.getStartInsertIndex());
      }

      return toInsert;
   }

   public String getValidConfigurationContent(String fullText, HashMap<String, List<String>> id_value_map) {
      Matcher match = WordCoreUtil.ELSE_PATTERN.matcher(fullText);
      String beginningText = fullText;
      String elseText = "";

      if (match.find()) {
         beginningText = fullText.substring(0, match.start());

         elseText = fullText.substring(match.end());
         elseText = elseText.replaceAll(WordCoreUtil.ENDCONFIG, "");
         elseText = elseText.replaceAll(WordCoreUtil.BEGINCONFIG, "");
      }

      String toReturn = "";

      // Note: this assumes only OR's are put in between configurations
      List<String> values = id_value_map.get(configurationToView.toUpperCase());
      if (values != null) {
         String value = values.get(0);
         if (!value.toLowerCase().equals("excluded")) {
            toReturn = beginningText;
         }
      } else {
         boolean isExcluded = false;
         for (Entry<String, List<String>> entry : id_value_map.entrySet()) {
            List<String> value = entry.getValue();
            isExcluded = value.get(0).toLowerCase().equals("excluded") ? true : false;
            if (!isExcluded) {
               break;
            }
         }
         toReturn = isExcluded ? beginningText : "";
      }
      return toReturn;
   }

   private String getValidFeatureContent(String fullText, boolean isInTable, HashMap<String, List<String>> featureIdValuesMap, ArrayList<String> featureOperators) {

      Matcher match = WordCoreUtil.ELSE_PATTERN.matcher(fullText);
      String beginningText = fullText;
      String elseText = "";

      if (match.find()) {

         if (isInTable) {
            String temp = fullText.substring(0, match.end());
            // Find last occurence of table row
            int lastIndexOf = temp.lastIndexOf(WordCoreUtil.START_TABLE_ROW);
            if (lastIndexOf != -1) {
               elseText = fullText.substring(lastIndexOf);
               elseText = elseText.replaceAll(WordCoreUtil.ELSE_EXP, "");
               beginningText = fullText.substring(0, lastIndexOf);
            }
         } else {
            beginningText = fullText.substring(0, match.start());
            elseText = fullText.substring(match.end());
         }

         elseText = elseText.replaceAll(WordCoreUtil.ENDFEATURE, "");
         elseText = elseText.replaceAll(WordCoreUtil.BEGINFEATURE, "");
      }

      String toReturn = "";
      String expression = createFeatureExpression(featureIdValuesMap, featureOperators);

      boolean result = false;
      try {
         result = (boolean) se.eval(expression);
      } catch (ScriptException ex) {
         logger.error("Failed to parse expression: " + expression);
      }

      if (result) {
         toReturn = beginningText;
      } else {
         toReturn = elseText;
      }

      return toReturn;
   }

   private String createFeatureExpression(HashMap<String, List<String>> featureIdValuesMap, ArrayList<String> featureOperators) {

      String myFeatureExpression = "";
      Iterator<String> iterator = featureOperators.iterator();

      for (String feature : featureIdValuesMap.keySet()) {
         List<String> values = featureIdValuesMap.get(feature);

         String valueExpression = createValueExpression(feature, values);

         boolean result = false;

         try {
            result = (boolean) se.eval(valueExpression);
         } catch (ScriptException ex) {
            logger.error("Failed to parse expression: " + valueExpression);
         }

         myFeatureExpression += result + " ";

         if (iterator.hasNext()) {
            String next = iterator.next();
            if (next.equals("|")) {
               myFeatureExpression += "|| ";
            } else if (next.equals("&")) {
               myFeatureExpression += "&& ";
            }
         }
      }

      return myFeatureExpression;
   }

   private String createValueExpression(String feature, List<String> values) {
      String myValueExpression = "";
      for (String value : values) {
         if (value.equals("(")) {
            myValueExpression += "( ";
         } else if (value.equals(")")) {
            myValueExpression += ") ";
         } else if (value.equals("|")) {
            myValueExpression += "|| ";
         } else if (value.equals("&")) {
            myValueExpression += "&& ";
         } else {
            boolean eval = isFeatureValuePairValid(feature, value);
            myValueExpression += eval + " ";
         }
      }

      return myValueExpression;
   }

   private boolean isFeatureValuePairValid(String feature, String value) {
      if (viewApplicabilitiesMap.containsKey(feature)) {
         Collection<String> validValues = viewApplicabilitiesMap.get(feature);

         value = value.equalsIgnoreCase("Default") ? getDefaultValue(feature) : value.trim();

         if (containsIgnoreCase(validValues, value)) {
            return true;
         }
      }

      return false;
   }

   private boolean containsIgnoreCase(Collection<String> validValues, String val) {
      for (String validValue : validValues) {
         if (validValue.equalsIgnoreCase(val)) {
            return true;
         }
      }
      return false;
   }

   private String getDefaultValue(String feature) {
      String toReturn = null;
      try {
         ObjectMapper mapper = new ObjectMapper();
         FeatureDefinitionData[] featDataList = mapper.readValue(featureDefinitionJson, FeatureDefinitionData[].class);

         for (FeatureDefinitionData featData : featDataList) {
            if (featData.getName().equalsIgnoreCase(feature)) {
               toReturn = featData.getDefaultValue();
               break;
            }
         }
      } catch (Exception e) {
         logger.error("Error getting default value for feature: " + feature);
      }

      return toReturn;
   }

   public static HashSet<String> getValidConfigurations(OrcsApi orcsApi, BranchId branch) {
      HashSet<String> validConfigurations = new HashSet<>();
      List<BranchViewData> branchViews = orcsApi.getQueryFactory().applicabilityQuery().getViews();

      for (BranchViewData branchView : branchViews) {

         ResultSet<ArtifactReadable> configs =
            orcsApi.getQueryFactory().fromBranch(branch).andIds(branchView.getBranchViews()).getResults();

         for (ArtifactReadable config : configs) {
            validConfigurations.add(config.getName());
         }
      }

      return validConfigurations;
   }
}

Back to the top