javaextensions/midppush/javasrc/com/nokia/mj/impl/push/PushUtil.java
changeset 79 2f468c1958d0
equal deleted inserted replaced
76:4ad59aaee882 79:2f468c1958d0
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 package com.nokia.mj.impl.push;
       
    19 
       
    20 import com.nokia.mj.impl.storage.StorageAttribute;
       
    21 import com.nokia.mj.impl.storage.StorageEntry;
       
    22 import com.nokia.mj.impl.storage.StorageNames;
       
    23 import com.nokia.mj.impl.storage.StorageSession;
       
    24 import com.nokia.mj.impl.utils.Uid;
       
    25 
       
    26 /**
       
    27  * Utility methods for handling push registrations.
       
    28  */
       
    29 public final class PushUtil
       
    30 {
       
    31     /**
       
    32      * Returns push registrations for specified application or
       
    33      * application suite.
       
    34      *
       
    35      * @param aSession StorageSesion to be used for fetching
       
    36      * the push registrations
       
    37      * @param aUid either application or application suite Uid
       
    38      * @return array of push registrations
       
    39      */
       
    40     public static PushRegInfo[] getPushRegs(StorageSession aSession, Uid aUid)
       
    41     {
       
    42         // Find push registrations using application uid.
       
    43         PushRegInfo[] pushRegs = getPushRegsForAppUid(aSession, aUid);
       
    44         if (pushRegs.length > 0)
       
    45         {
       
    46             return pushRegs;
       
    47         }
       
    48 
       
    49         // Push registrations not found, so find application uids using
       
    50         // suite uid and then push registrations using application uids.
       
    51         Uid[] appUids = getAppUids(aSession, aUid);
       
    52         PushRegInfo[][] pushRegArray = new PushRegInfo[appUids.length][];
       
    53         int pushRegCount = 0;
       
    54         for (int i = 0; i < appUids.length; i++)
       
    55         {
       
    56             pushRegArray[i] = getPushRegs(aSession, appUids[i]);
       
    57             pushRegCount += pushRegArray[i].length;
       
    58         }
       
    59         pushRegs = new PushRegInfo[pushRegCount];
       
    60         int pushRegsIndex = 0;
       
    61         for (int i = 0; i < pushRegArray.length; i++)
       
    62         {
       
    63             for (int j = 0; j < pushRegArray[i].length; j++)
       
    64             {
       
    65                 pushRegs[pushRegsIndex++] = pushRegArray[i][j];
       
    66             }
       
    67         }
       
    68         return pushRegs;
       
    69     }
       
    70 
       
    71     /**
       
    72      * Returns push registrations for specified application.
       
    73      *
       
    74      * @param aSession StorageSesion to be used for fetching
       
    75      * the push registrations
       
    76      * @param aUid application Uid
       
    77      * @return array of push registrations
       
    78      */
       
    79     private static PushRegInfo[] getPushRegsForAppUid(StorageSession aSession, Uid aUid)
       
    80     {
       
    81         PushRegInfo[] pushRegs = new PushRegInfo[0];
       
    82         StorageEntry query = new StorageEntry();
       
    83         query.addAttribute(
       
    84             new StorageAttribute(StorageNames.ID, aUid.getStringValue()));
       
    85         StorageEntry[] entries = aSession.search(
       
    86             StorageNames.PUSH_REGISTRATIONS_TABLE, query);
       
    87         if (entries != null && entries.length > 0)
       
    88         {
       
    89             pushRegs = new PushRegInfo[entries.length];
       
    90             for (int i = 0; i < entries.length; i++)
       
    91             {
       
    92                 Uid uid = Uid.createUid(
       
    93                     entries[i].getAttribute(StorageNames.ID).getValue());
       
    94                 String name = entries[i].getAttribute(StorageNames.NAME).getValue();
       
    95                 String url = entries[i].getAttribute(StorageNames.URL).getValue();
       
    96                 String filter = entries[i].getAttribute(StorageNames.FILTER).getValue();
       
    97                 int regType = Integer.parseInt(
       
    98                     entries[i].getAttribute(StorageNames.REGISTRATION_TYPE).getValue());
       
    99                 pushRegs[i] = new PushRegInfo(uid, name, url, filter, regType);
       
   100             }
       
   101         }
       
   102         return pushRegs;
       
   103     }
       
   104 
       
   105     /**
       
   106      * Returns application Uids for specified application suite.
       
   107      *
       
   108      * @param aSession StorageSesion to be used for fetching the Uids
       
   109      * @param aSuiteUid application suite Uid
       
   110      * @return array of application Uids
       
   111      */
       
   112     private static Uid[] getAppUids(StorageSession aSession, Uid aUid)
       
   113     {
       
   114         Uid[] uids = new Uid[0];
       
   115         StorageEntry query = new StorageEntry();
       
   116         query.addAttribute(new StorageAttribute(
       
   117                                StorageNames.PACKAGE_ID, aUid.getStringValue()));
       
   118         query.addAttribute(new StorageAttribute(StorageNames.ID, ""));
       
   119         StorageEntry[] entries =
       
   120             aSession.search(StorageNames.APPLICATION_TABLE, query);
       
   121         if (entries != null && entries.length > 0)
       
   122         {
       
   123             uids = new Uid[entries.length];
       
   124             for (int i = 0; i < entries.length; i++)
       
   125             {
       
   126                 uids[i] = Uid.createUid(
       
   127                     entries[i].getAttribute(StorageNames.ID).getValue());
       
   128             }
       
   129         }
       
   130         return uids;
       
   131     }
       
   132 
       
   133     /**
       
   134      * PushRegInfo contains information of one push registration.
       
   135      */
       
   136     public static final class PushRegInfo
       
   137     {
       
   138         /** Dynamic push registration. */
       
   139         public static final int DYNAMIC = 0;
       
   140         /** Static push registration. */
       
   141         public static final int STATIC = 1;
       
   142 
       
   143         /** Application Uid. */
       
   144         private Uid iUid = null;
       
   145         /** Application name. */
       
   146         private String iName = null;
       
   147         /** Push registration URL */
       
   148         private String iUrl = null;
       
   149         /** Filter for incoming messages/connections. */
       
   150         private String iFilter = null;
       
   151         /** Registration type. Either DYNAMIC or STATIC. */
       
   152         private int iRegType = DYNAMIC;
       
   153 
       
   154         PushRegInfo(
       
   155             Uid aUid, String aName, String aUrl, String aFilter, int aRegType)
       
   156         {
       
   157             iUid = aUid;
       
   158             iName =  aName;
       
   159             iUrl = aUrl;
       
   160             iFilter = aFilter;
       
   161             iRegType = aRegType;
       
   162         }
       
   163 
       
   164         /** Returns application Uid. */
       
   165         public Uid getUid()
       
   166         {
       
   167             return iUid;
       
   168         }
       
   169 
       
   170         /** Returns application name. */
       
   171         public String getName()
       
   172         {
       
   173             return iName;
       
   174         }
       
   175 
       
   176         /** Returns push registration URL */
       
   177         public String getUrl()
       
   178         {
       
   179             return iUrl;
       
   180         }
       
   181 
       
   182         /** Returns filter for incoming messages/connections. */
       
   183         public String getFilter()
       
   184         {
       
   185             return iFilter;
       
   186         }
       
   187 
       
   188         /** Returns registration type. Either DYNAMIC or STATIC. */
       
   189         public int getRegType()
       
   190         {
       
   191             return iRegType;
       
   192         }
       
   193     }
       
   194 }