Skip to main content
summaryrefslogtreecommitdiffstats
blob: b57f99f1cf57d00a6314effb81495b09a54e447c (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
/*******************************************************************************
 * Copyright (c) 2013 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.orcs.account.admin.internal;

import java.io.StringReader;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import org.eclipse.osee.account.admin.AccountPreferences;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.jdk.core.type.BaseIdentity;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.PropertyStore;
import org.eclipse.osee.orcs.data.ArtifactReadable;

/**
 * @author Roberto E. Escobar
 */
public class AccountPreferencesArtifact extends BaseIdentity<String>implements AccountPreferences {

   private final ArtifactReadable artifact;

   private Map<String, String> data = Collections.emptyMap();
   private final AtomicBoolean wasLoaded = new AtomicBoolean(false);

   public AccountPreferencesArtifact(String uuid, ArtifactReadable artifact) {
      super(uuid);
      this.artifact = artifact;
   }

   @Override
   public long getId() {
      return artifact.getUuid();
   }

   @Override
   public String get(String key) {
      checkLoaded();
      return data.get(key);
   }

   @Override
   public boolean getBoolean(String key) {
      checkLoaded();
      String value = data.get(key);
      return Boolean.valueOf(value);
   }

   @Override
   public Set<String> getKeys() {
      checkLoaded();
      return data.keySet();
   }

   @Override
   public Map<String, String> asMap() {
      checkLoaded();
      return Collections.unmodifiableMap(data);
   }

   private void checkLoaded() {
      if (wasLoaded.compareAndSet(false, true)) {
         String settings = artifact.getSoleAttributeValue(CoreAttributeTypes.UserSettings, null);
         if (settings != null) {
            PropertyStore storage = new PropertyStore(artifact.getGuid());
            try {
               storage.load(new StringReader(settings));
            } catch (Exception ex) {
               throw OseeCoreException.wrap(ex);
            }
            Map<String, String> map = new HashMap<>();
            for (String key : storage.keySet()) {
               map.put(key, storage.get(key));
            }
            data = map;
         }
      }
   }

}

Back to the top