Skip to main content
summaryrefslogtreecommitdiffstats
blob: 40649a2714b2a8f84666227bb7258111e42e237f (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
/*******************************************************************************
 * 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.ats.util.internal;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import java.util.concurrent.TimeUnit;
import org.eclipse.osee.ats.api.config.AtsConfigurations;
import org.eclipse.osee.ats.api.config.IAtsConfigurationsService;
import org.eclipse.osee.ats.api.user.AtsUser;
import org.eclipse.osee.ats.api.user.IAtsUser;
import org.eclipse.osee.ats.api.user.IUserArtLoader;
import org.eclipse.osee.ats.internal.AtsClientService;
import org.eclipse.osee.framework.core.data.ArtifactToken;
import org.eclipse.osee.framework.core.exception.ArtifactDoesNotExist;
import org.eclipse.osee.framework.skynet.core.UserManager;
import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery;

/**
 * Provides client configurations through server endpoint
 *
 * @author Donald G. Dunne
 */
public class AtsConfigurationsService implements IAtsConfigurationsService {

   @Override
   public AtsConfigurations getConfigurations() {
      return configurationsCache.get();
   }

   @Override
   public AtsConfigurations getConfigurationsWithPend() {
      return getConfigurationsSupplier().get();
   }

   private final Supplier<AtsConfigurations> configurationsCache =
      Suppliers.memoizeWithExpiration(getConfigurationsSupplier(), 5, TimeUnit.MINUTES);

   private Supplier<AtsConfigurations> getConfigurationsSupplier() {
      return new Supplier<AtsConfigurations>() {
         @Override
         public AtsConfigurations get() {
            return loadConfigurations();
         }
      };
   }

   private AtsConfigurations loadConfigurations() {
      AtsConfigurations configs = AtsClientService.getConfigEndpoint().get();
      for (IAtsUser user : configs.getUsers()) {
         AtsUser jUser = (AtsUser) user;
         jUser.setUserArtLoader(userLoader);
      }
      return configs;
   }

   /**
    * Lazy Loader for user artifact
    */
   private final UserArtLoader userLoader = new UserArtLoader();
   private class UserArtLoader implements IUserArtLoader {

      @Override
      public ArtifactToken loadUser(IAtsUser user) {
         ArtifactToken userArt = null;
         try {
            userArt = UserManager.getUserByArtId(user.getId());
            if (userArt == null) {
               userArt = ArtifactQuery.getArtifactFromId(user.getId(), AtsClientService.get().getAtsBranch());
            }
         } catch (ArtifactDoesNotExist ex) {
            // do nothing
         }
         user.setStoreObject(userArt);
         return userArt;
      }
   }
}

Back to the top