Coverage Report - pt.digitalis.dif.dem.managers.impl.UsageIssuesManagerImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
UsageIssuesManagerImpl
0%
0/77
0%
0/68
4,273
 
 1  
 /**
 2  
  * 2008, Digitalis Informatica. All rights reserved. Distribuicao e Gestao de Informatica, Lda. Estrada de Paco de Arcos
 3  
  * num.9 - Piso -1 2780-666 Paco de Arcos Telefone: (351) 21 4408990 Fax: (351) 21 4408999 http://www.digitalis.pt
 4  
  */
 5  
 
 6  
 package pt.digitalis.dif.dem.managers.impl;
 7  
 
 8  
 import java.util.ArrayList;
 9  
 import java.util.HashMap;
 10  
 import java.util.List;
 11  
 import java.util.Map;
 12  
 
 13  
 import pt.digitalis.dif.dem.managers.IUsageIssuesManager;
 14  
 import pt.digitalis.dif.dem.objects.issues.IssueScope;
 15  
 import pt.digitalis.dif.dem.objects.issues.IssueType;
 16  
 import pt.digitalis.dif.dem.objects.issues.UsageIssue;
 17  
 import pt.digitalis.dif.utils.logging.DIFLogger;
 18  
 import pt.digitalis.utils.common.StringUtils;
 19  
 
 20  
 /**
 21  
  * The user issue manager implementation. Singleton pattern.
 22  
  * 
 23  
  * @author Rodrigo Gonçalves <a href="mailto:rgoncalves@digitalis.pt">rgoncalves@digitalis.pt</a><br/>
 24  
  * @created 2008/11/13
 25  
  */
 26  0
 public class UsageIssuesManagerImpl implements IUsageIssuesManager {
 27  
 
 28  
     /** The singleton instance. */
 29  0
     final static private IUsageIssuesManager INSTANCE = new UsageIssuesManagerImpl();
 30  
 
 31  
     /**
 32  
      * Singleton pattern instance accessor.
 33  
      * 
 34  
      * @return the instance
 35  
      */
 36  
     static public IUsageIssuesManager getInstance()
 37  
     {
 38  0
         return INSTANCE;
 39  
     }
 40  
 
 41  
     /** Flag that controls if there are issues */
 42  0
     private boolean hasIssues = false;
 43  
 
 44  
     /** The issues map. <K=location, V=issue set> */
 45  0
     private Map<String, List<UsageIssue>> issues = new HashMap<String, List<UsageIssue>>();
 46  
 
 47  
     /** Private ctor. */
 48  0
     private UsageIssuesManagerImpl()
 49  0
     {}
 50  
 
 51  
     /**
 52  
      * @see pt.digitalis.dif.dem.managers.IUsageIssuesManager#addIssue(pt.digitalis.dif.dem.objects.issues.IssueType,
 53  
      *      pt.digitalis.dif.dem.objects.issues.IssueScope, java.lang.String, java.lang.String, java.lang.Exception)
 54  
      */
 55  
     public void addIssue(IssueType issueType, IssueScope issueScope, String location, String issueDescription,
 56  
             Exception exception)
 57  
     {
 58  
 
 59  0
         UsageIssue issue = new UsageIssue();
 60  
 
 61  0
         if (issueType != null)
 62  0
             issue.setIssueType(issueType);
 63  
 
 64  0
         if (issueScope != null)
 65  0
             issue.setIssueScope(issueScope);
 66  
 
 67  0
         if (location != null)
 68  0
             issue.setLocation(location);
 69  
 
 70  0
         if (issueDescription != null)
 71  0
             issue.setIssueDescription(issueDescription);
 72  
 
 73  0
         if (exception != null)
 74  0
             issue.setException(exception);
 75  
 
 76  0
         this.addIssue(issue);
 77  0
     }
 78  
 
 79  
     /**
 80  
      * @see pt.digitalis.dif.dem.managers.IUsageIssuesManager#addIssue(pt.digitalis.dif.dem.objects.issues.UsageIssue)
 81  
      */
 82  
     public void addIssue(UsageIssue usageIssue)
 83  
     {
 84  
         // Check if the given UID exists, if not create it
 85  0
         if (!getIssues().containsKey(usageIssue.getLocation()))
 86  0
             getIssues().put(usageIssue.getLocation(), new ArrayList<UsageIssue>());
 87  
 
 88  
         // Add the issue
 89  0
         boolean addIssue = true;
 90  
 
 91  
         // Validate issue UID if defined. Prevent duplicate reportings...
 92  0
         if (StringUtils.isNotBlank(usageIssue.getUID()))
 93  0
             for (UsageIssue existingIssue: getIssues().get(usageIssue.getLocation()))
 94  0
                 if (usageIssue.getUID().equals(existingIssue.getUID()))
 95  
                 {
 96  0
                     addIssue = false;
 97  0
                     break;
 98  
                 }
 99  
 
 100  0
         if (addIssue)
 101  0
             getIssues().get(usageIssue.getLocation()).add(usageIssue);
 102  
 
 103  0
         if (!hasIssues)
 104  0
             hasIssues = true;
 105  0
     }
 106  
 
 107  
     /**
 108  
      * @see pt.digitalis.dif.dem.managers.IUsageIssuesManager#getIssues()
 109  
      */
 110  
     public Map<String, List<UsageIssue>> getIssues()
 111  
     {
 112  0
         return this.issues;
 113  
     }
 114  
 
 115  
     /**
 116  
      * @see pt.digitalis.dif.dem.managers.IUsageIssuesManager#getIssues(java.lang.String)
 117  
      */
 118  
     public List<UsageIssue> getIssues(String entityUID)
 119  
     {
 120  0
         if (getIssues().containsKey(entityUID))
 121  0
             return getIssues().get(entityUID);
 122  
         else
 123  0
             return null;
 124  
     }
 125  
 
 126  
     /**
 127  
      * @see pt.digitalis.dif.dem.managers.IUsageIssuesManager#hasIssues()
 128  
      */
 129  
     public boolean hasIssues()
 130  
     {
 131  0
         return this.hasIssues;
 132  
     }
 133  
 
 134  
     /**
 135  
      * @see pt.digitalis.dif.dem.managers.IUsageIssuesManager#hasIssues(java.lang.String)
 136  
      */
 137  
     public boolean hasIssues(String location)
 138  
     {
 139  0
         if (hasIssues())
 140  
         {
 141  
             // One issue is "issues"
 142  0
             if (this.getIssues().get(location) != null && this.getIssues().get(location).size() > 0)
 143  0
                 return true;
 144  
         }
 145  0
         return false;
 146  
     }
 147  
 
 148  
     /**
 149  
      * @see pt.digitalis.dif.dem.managers.IUsageIssuesManager#logAllIssuesByEntity()
 150  
      */
 151  
     public void logAllIssuesByEntity()
 152  
     {
 153  
         // Log only if there's data
 154  0
         if (getIssues().size() > 0)
 155  
         {
 156  0
             for (String uid: getIssues().keySet())
 157  
             {
 158  0
                 logIssues(uid);
 159  
             }
 160  
         }
 161  0
     }
 162  
 
 163  
     /**
 164  
      * @see pt.digitalis.dif.dem.managers.IUsageIssuesManager#logAllIssuesByType()
 165  
      */
 166  
     public void logAllIssuesByType()
 167  
     {
 168  0
         if (getIssues().size() > 0)
 169  
         {
 170  0
             List<UsageIssue> errorIssues = new ArrayList<UsageIssue>();
 171  0
             List<UsageIssue> warnIssues = new ArrayList<UsageIssue>();
 172  0
             List<UsageIssue> infoIssues = new ArrayList<UsageIssue>();
 173  
 
 174  0
             for (String uid: getIssues().keySet())
 175  
             {
 176  0
                 for (UsageIssue issue: getIssues(uid))
 177  
                 {
 178  0
                     if (issue.getIssueType().equals(IssueType.ERROR))
 179  0
                         errorIssues.add(issue);
 180  0
                     else if (issue.getIssueType().equals(IssueType.WARN))
 181  0
                         warnIssues.add(issue);
 182  0
                     else if (issue.getIssueType().equals(IssueType.INFO))
 183  0
                         infoIssues.add(issue);
 184  
                 }
 185  
             }
 186  
 
 187  0
             if (errorIssues.size() > 0)
 188  0
                 for (UsageIssue issue: errorIssues)
 189  0
                     DIFLogger.getLogger().error(issue.toString());
 190  
 
 191  0
             if (warnIssues.size() > 0)
 192  0
                 for (UsageIssue issue: warnIssues)
 193  0
                     DIFLogger.getLogger().warn(issue.toString());
 194  
 
 195  0
             if (infoIssues.size() > 0)
 196  0
                 for (UsageIssue issue: infoIssues)
 197  0
                     DIFLogger.getLogger().info(issue.toString());
 198  
 
 199  
         }
 200  0
     }
 201  
 
 202  
     /**
 203  
      * @see pt.digitalis.dif.dem.managers.IUsageIssuesManager#logIssues(java.lang.String)
 204  
      */
 205  
     public void logIssues(String entityUID)
 206  
     {
 207  
         // Log only if there's data
 208  0
         if (getIssues(entityUID).size() > 0)
 209  
         {
 210  0
             for (UsageIssue issue: getIssues(entityUID))
 211  
             {
 212  0
                 if (issue.getIssueType().equals(IssueType.ERROR))
 213  
                 {
 214  0
                     DIFLogger.getLogger().error(issue.toString());
 215  
                 }
 216  0
                 else if (issue.getIssueType().equals(IssueType.WARN))
 217  
                 {
 218  0
                     DIFLogger.getLogger().warn(issue.toString());
 219  
                 }
 220  0
                 else if (issue.getIssueType().equals(IssueType.INFO))
 221  
                 {
 222  0
                     DIFLogger.getLogger().info(issue.toString());
 223  
                 }
 224  
             }
 225  
         }
 226  0
     }
 227  
 }