/**
   * Retrieves a group of user preferences from persistent storage. Call with userPrefGroupTypeId
   * and optional userPrefLoginId. If userPrefLoginId isn't specified, then the currently logged-in
   * user's userLoginId will be used. The retrieved preferences group is contained in the
   * <b>userPrefMap</b> element.
   *
   * @param ctx The DispatchContext that this service is operating in.
   * @param context Map containing the input arguments.
   * @return Map with the result of the service, the output parameters.
   */
  public static Map<String, Object> getUserPreferenceGroup(
      DispatchContext ctx, Map<String, ?> context) {
    Locale locale = (Locale) context.get("locale");
    if (!PreferenceWorker.isValidGetId(ctx, context)) {
      return ServiceUtil.returnError(
          UtilProperties.getMessage(resource, "getPreference.permissionError", locale));
    }
    Delegator delegator = ctx.getDelegator();

    String userPrefGroupTypeId = (String) context.get("userPrefGroupTypeId");
    if (UtilValidate.isEmpty(userPrefGroupTypeId)) {
      return ServiceUtil.returnError(
          UtilProperties.getMessage(resource, "getPreference.invalidArgument", locale));
    }
    String userLoginId = PreferenceWorker.getUserLoginId(context, true);

    Map<String, Object> userPrefMap = null;
    try {
      Map<String, String> fieldMap =
          UtilMisc.toMap("userLoginId", "_NA_", "userPrefGroupTypeId", userPrefGroupTypeId);
      userPrefMap =
          PreferenceWorker.createUserPrefMap(delegator.findByAnd("UserPreference", fieldMap));
      fieldMap.put("userLoginId", userLoginId);
      userPrefMap.putAll(
          PreferenceWorker.createUserPrefMap(delegator.findByAnd("UserPreference", fieldMap)));
    } catch (GenericEntityException e) {
      Debug.logWarning(e.getMessage(), module);
      return ServiceUtil.returnError(
          UtilProperties.getMessage(
              resource, "getPreference.readFailure", new Object[] {e.getMessage()}, locale));
    } catch (GeneralException e) {
      Debug.logWarning(e.getMessage(), module);
      return ServiceUtil.returnError(
          UtilProperties.getMessage(
              resource, "getPreference.readFailure", new Object[] {e.getMessage()}, locale));
    }
    // for the 'DEFAULT' values find the related values in general properties and if found use
    // those.
    Iterator it = userPrefMap.entrySet().iterator();
    Map generalProperties = UtilProperties.getProperties("general");
    while (it.hasNext()) {
      Map.Entry pairs = (Map.Entry) it.next();
      if ("DEFAULT".equals(pairs.getValue())) {
        if (UtilValidate.isNotEmpty(generalProperties.get(pairs.getKey()))) {
          userPrefMap.put((String) pairs.getKey(), generalProperties.get(pairs.getKey()));
        }
      }
    }

    Map<String, Object> result = ServiceUtil.returnSuccess();
    result.put("userPrefMap", userPrefMap);
    return result;
  }
Exemple #2
0
 private static void initConfig(URL url) throws IOException {
   try {
     if (url == null) {
       String errMsg =
           "The Java environment (-Dxxx=yyy) variable with name "
               + url.toString()
               + " is not set, cannot resolve location.";
       throw new MalformedURLException(errMsg);
     }
     props = UtilProperties.getProperties(url);
   } catch (IOException e) {
     e.printStackTrace();
     throw e;
   }
 }
  /** Reloads asterisk config properties. */
  public void reload() {
    // retrieve asterisk manager configuration here
    Properties configProperties = UtilProperties.getProperties("asterisk.properties");
    // asterisk host address
    String host = (String) configProperties.get("asterisk.host");
    // asterisk manager username
    String username = (String) configProperties.get("asterisk.username");
    // asterisk manager password
    String password = (String) configProperties.get("asterisk.password");
    // asterisk dial out prefix, calls between internal extensions are made by dialing the extension
    // number
    // whereas external calls need a prefix before the number you want dial, such as 9
    asteriskExternalCode = (String) configProperties.get("asterisk.outbound.prev");
    // the country code of asterisk server, can be specified to avoid dialing national numbers using
    // the country code
    asteriskCountryCode =
        UtilProperties.getPropertyValue("asterisk.properties", "asterisk.countryCode", "");
    // the area code of asterisk server, can be specified to avoid dialing local area numbers using
    // the area code
    asteriskAreaCode =
        UtilProperties.getPropertyValue("asterisk.properties", "asterisk.areaCode", "");
    // the current phone number of the asterisk server (the phone number of the phone line connected
    // to the asterisk server)
    asteriskPhoneNumber =
        UtilProperties.getPropertyValue("asterisk.properties", "asterisk.phoneNumber", "");
    // the prefix used to dial international phone numbers, usually "011" or "00" ... depends on the
    // origin country
    asteriskInternationalPrefix =
        UtilProperties.getPropertyValue("asterisk.properties", "asterisk.outbound.foreign", "");
    // the prefix used to dial numbers out of the local area, for example to dial other cities /
    // states
    asteriskAreaPrefix =
        UtilProperties.getPropertyValue("asterisk.properties", "asterisk.outbound.area", "");
    // if always add area code to dial, even though it was the same area code.
    alwaysDialAreaCode =
        UtilProperties.getPropertyValue("asterisk.properties", "asterisk.alwaysDialAreaCode", "Y");
    // if always add country code to dial, even though it was the same country code, it used in some
    // voip provider.
    alwaysDialCountryCode =
        UtilProperties.getPropertyValue(
            "asterisk.properties", "asterisk.alwaysDialCountryCode", "Y");
    // Dialplan context to use for outgoing calls, i.e. the dialed number
    originationContext =
        UtilProperties.getPropertyValue(
            "asterisk.properties", "asterisk.originationContext", "from-internal");
    // Channel Technology to use for outgoing calls for the user's channel i.e. SIP/ for chan_sip or
    // Local/ for chan_local
    originationChannel =
        UtilProperties.getPropertyValue(
            "asterisk.properties", "asterisk.originationChannel", "Local/");
    // Channel Technology suffix (useful for ChanLocal - i.e. Local/825@from-internal
    originationChannelSuffix =
        UtilProperties.getPropertyValue(
            "asterisk.properties", "asterisk.originationChannelSuffix", "");
    // Area codes that don't need the long-distance prefix
    String localAreaCodesString =
        UtilProperties.getPropertyValue("asterisk.properties", "asterisk.localAreaCodes", "");
    localAreaCodes = UtilMisc.toListArray(localAreaCodesString.split(","));
    // logs out an existing connection
    if (managerConnection != null) {
      if (managerConnection.getState().equals(ManagerConnectionState.CONNECTED)) {
        managerConnection.logoff();
      }
    }

    ManagerConnectionFactory factory = new ManagerConnectionFactory(host, username, password);

    this.managerConnection = factory.createManagerConnection();
    // connect to Asterisk and log in
    try {
      managerConnection.login();
      managerConnection.addEventListener(
          new ManagerEventListener() {
            public void onManagerEvent(ManagerEvent event) {
              // add a listener to handle incoming and outgoing calls
              // e.getCallerId() is the calling phone number. such as 8605758672106
              // e.getDestination() is the destination of call, include asterisk communication, such
              // as SIP/825-09caf850
              if (event instanceof DialEvent) {
                DialEvent e = (DialEvent) event;
                String destinationNumber = retrieveNumber(e.getDestination());
                Debug.logInfo("Call from:" + e.getCallerId() + ", to:" + destinationNumber, MODULE);
                calls.put(destinationNumber, e.getCallerId());
              }
            }
          });
      managerConnection.sendAction(new StatusAction());
    } catch (IllegalStateException e) {
      Debug.logError(e, "Error reloading asterisk server manager connection", MODULE);
    } catch (IOException e) {
      Debug.logError(e, "Error reloading asterisk server manager connection", MODULE);
    } catch (AuthenticationFailedException e) {
      Debug.logError(e, "Error reloading asterisk server manager connection", MODULE);
    } catch (TimeoutException e) {
      Debug.logError(e, "Error reloading asterisk server manager connection", MODULE);
    }
  }