Skip to main content
summaryrefslogtreecommitdiffstats
blob: 569173afc6ab87b0b518b071fbc62d73301dfe62 (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) 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.access.internal.data;

import java.util.HashMap;
import java.util.Map;
import org.eclipse.osee.framework.access.AccessObject;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.database.core.ConnectionHandler;
import org.eclipse.osee.framework.skynet.core.artifact.BranchManager;

/**
 * @author Jeff C. Phillips
 */
public class BranchAccessObject extends AccessObject {
   private final int branchId;
   private static final Map<Integer, BranchAccessObject> cache = new HashMap<Integer, BranchAccessObject>();

   @Override
   public int hashCode() {
      int result = 17;
      result = 31 * result + branchId;
      return result;
   }

   public BranchAccessObject(int branchId) {
      this.branchId = branchId;
   }

   @Override
   public int getId() {
      return branchId;
   }

   @Override
   public void removeFromCache() {
      cache.remove(branchId);
   }

   @Override
   public void removeFromDatabase(int subjectId) throws OseeCoreException {
      final String DELETE_BRANCH_ACL = "DELETE FROM OSEE_BRANCH_ACL WHERE privilege_entity_id = ? AND branch_id =?";
      ConnectionHandler.runPreparedUpdate(DELETE_BRANCH_ACL, subjectId, branchId);
   }

   public static BranchAccessObject getBranchAccessObject(IOseeBranch branch) throws OseeCoreException {
      return getBranchAccessObject(BranchManager.getBranchId(branch));
   }

   public static BranchAccessObject getBranchAccessObject(String branchGuid) throws OseeCoreException {
      if (BranchManager.branchExists(branchGuid)) {
         Branch branch = BranchManager.getBranchByGuid(branchGuid);
         return getBranchAccessObject(branch);
      }
      return null;
   }

   public static BranchAccessObject getBranchAccessObject(int branchId) {
      BranchAccessObject branchAccessObject;
      if (cache.containsKey(branchId)) {
         branchAccessObject = cache.get(branchId);
      } else {
         branchAccessObject = new BranchAccessObject(branchId);
         cache.put(branchId, branchAccessObject);
      }
      return branchAccessObject;
   }

   public static BranchAccessObject getBranchAccessObjectFromCache(IOseeBranch branch) throws OseeCoreException {
      return cache.get(BranchManager.getBranchId(branch));
   }

   @Override
   public boolean equals(Object obj) {
      if (!(obj instanceof BranchAccessObject)) {
         return false;
      }
      return branchId == ((BranchAccessObject) obj).branchId;
   }
}

Back to the top