Skip to main content
summaryrefslogtreecommitdiffstats
blob: b761845dbf57995ed04b0e64b98487edf10f2728 (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
/*******************************************************************************
 * Copyright (c) 2004, 2007 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.framework.core.message.internal.translation;

import java.util.Collection;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.data.NamedIdentity;
import org.eclipse.osee.framework.core.enums.DeletionFlag;
import org.eclipse.osee.framework.core.message.SearchOptions;
import org.eclipse.osee.framework.core.message.SearchRequest;
import org.eclipse.osee.framework.core.translation.ITranslator;
import org.eclipse.osee.framework.jdk.core.type.PropertyStore;
import org.eclipse.osee.framework.jdk.core.util.Strings;

/**
 * @author Roberto E. Escobar
 */
public class SearchRequestTranslator implements ITranslator<SearchRequest> {

   private static enum Entry {
      BRANCH_GUID,
      BRANCH_NAME,
      RAW_SEARCH,
      OPTION_IS_CASE_SENSITIVE,
      OPTION_MATCH_WORD_ORDER,
      OPTION_IS_INCLUDE_DELETED,
      OPTION_FIND_ALL_LOCATIONS,
      OPTION_ATTRIBUTE_TYPE_FILTER_GUIDS,
      OPTION_ATTRIBUTE_TYPE_FILTER_NAMES
   }

   @Override
   public SearchRequest convert(PropertyStore store) {
      String guid = store.get(Entry.BRANCH_GUID.name());
      String name = store.get(Entry.BRANCH_NAME.name());
      IOseeBranch branch = new BranchToken(guid, name);

      String rawSearch = store.get(Entry.RAW_SEARCH.name());
      SearchOptions options = new SearchOptions();

      options.setCaseSensive(store.getBoolean(Entry.OPTION_IS_CASE_SENSITIVE.name()));
      options.setFindAllLocationsEnabled(store.getBoolean(Entry.OPTION_FIND_ALL_LOCATIONS.name()));
      options.setMatchWordOrder(store.getBoolean(Entry.OPTION_MATCH_WORD_ORDER.name()));

      boolean areDeletedAllowed = store.getBoolean(Entry.OPTION_IS_INCLUDE_DELETED.name());
      options.setDeletedIncluded(DeletionFlag.allowDeleted(areDeletedAllowed));

      String[] typeFilterGuids = store.getArray(Entry.OPTION_ATTRIBUTE_TYPE_FILTER_GUIDS.name());
      String[] typeFilterNames = store.getArray(Entry.OPTION_ATTRIBUTE_TYPE_FILTER_NAMES.name());
      if (typeFilterGuids.length > 0 && typeFilterNames.length > 0) {
         for (int index = 0; index < typeFilterGuids.length; index++) {
            guid = typeFilterGuids[index];
            name = index < typeFilterNames.length ? typeFilterNames[index] : Strings.emptyString();
            IAttributeType type = new AttributeTypeFilter(guid, name);
            options.addAttributeTypeFilter(type);
         }
      }
      return new SearchRequest(branch, rawSearch, options);
   }

   @Override
   public PropertyStore convert(SearchRequest object) {
      PropertyStore store = new PropertyStore();
      IOseeBranch branch = object.getBranch();

      store.put(Entry.BRANCH_GUID.name(), branch.getGuid());
      store.put(Entry.BRANCH_NAME.name(), branch.getName());

      store.put(Entry.RAW_SEARCH.name(), object.getRawSearch());
      SearchOptions options = object.getOptions();
      if (options != null) {
         store.put(Entry.OPTION_IS_CASE_SENSITIVE.name(), options.isCaseSensitive());
         store.put(Entry.OPTION_MATCH_WORD_ORDER.name(), options.isMatchWordOrder());
         store.put(Entry.OPTION_IS_INCLUDE_DELETED.name(), options.getDeletionFlag().areDeletedAllowed());
         store.put(Entry.OPTION_FIND_ALL_LOCATIONS.name(), options.isFindAllLocationsEnabled());

         if (options.isAttributeTypeFiltered()) {
            Collection<IAttributeType> types = options.getAttributeTypeFilter();
            String[] guids = new String[types.size()];
            String[] names = new String[types.size()];
            int index = 0;
            for (IAttributeType type : types) {
               guids[index] = type.getGuid();
               names[index] = type.getName();
               index++;
            }
            store.put(Entry.OPTION_ATTRIBUTE_TYPE_FILTER_GUIDS.name(), guids);
            store.put(Entry.OPTION_ATTRIBUTE_TYPE_FILTER_NAMES.name(), names);
         }
      }
      return store;
   }

   private static final class BranchToken extends NamedIdentity implements IOseeBranch {
      public BranchToken(String guid, String name) {
         super(guid, name);
      }
   }

   private static final class AttributeTypeFilter extends NamedIdentity implements IAttributeType {
      public AttributeTypeFilter(String guid, String name) {
         super(guid, name);
      }
   }
}

Back to the top