Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9349759c4f0bf2f9a49bb3d4ac9b871b9a36cbfe (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.skynet.core.artifact;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import org.eclipse.osee.framework.core.exception.OseeArgumentException;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.core.exception.OseeStateException;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.cache.AbstractOseeCache;
import org.eclipse.osee.framework.jdk.core.util.GUID;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.Strings;

/**
 * @author Roberto E. Escobar
 */
public final class BranchUtility {

   public static String toFileName(Branch branch) throws OseeCoreException {
      if (branch == null) {
         throw new OseeArgumentException("branch cannot be null");
      }
      String branchGuid = branch.getGuid();
      if (!GUID.isValid(branchGuid)) {
         throw new OseeStateException("GUID for branch [%s] is invalid", branch.getId());
      }
      return encode(branchGuid);
   }

   private static String encode(String name) throws OseeCoreException {
      String toReturn = "";
      try {
         toReturn = URLEncoder.encode(name, "UTF-8");
      } catch (Exception ex) {
         if (ex instanceof OseeCoreException) {
            throw (OseeCoreException) ex;
         } else {
            OseeExceptions.wrapAndThrow(ex);
         }
      }
      return toReturn;
   }

   public static Branch fromFileName(AbstractOseeCache<Branch> cache, String fileName) throws OseeCoreException {
      if (cache == null) {
         throw new OseeArgumentException("cache cannot be null");
      }
      if (!Strings.isValid(fileName)) {
         throw new OseeArgumentException("file name cannot be null or empty");
      }
      Branch toReturn = null;
      String branchGuid = decode(fileName);
      if (GUID.isValid(branchGuid)) {
         toReturn = cache.getByGuid(branchGuid);
         if (toReturn == null) {
            throw new OseeArgumentException("Unable to find branch matching guid [%s]", branchGuid);
         }
      } else {
         int branchId = Integer.parseInt(Lib.getExtension(fileName));
         toReturn = cache.getById(branchId);
         if (toReturn == null) {
            throw new OseeArgumentException("Unable to find branch matching id [%s]", branchId);
         }
      }
      return toReturn;
   }

   private static String decode(String name) {
      String toReturn = name;
      try {
         toReturn = URLDecoder.decode(name, "UTF-8");
      } catch (UnsupportedEncodingException ex) {
         // Do Nothing
      }
      return toReturn;
   }
}

Back to the top