Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2014 Philip Helger (www.helger.com) philip[at]helger[dot]com Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package com.helger.appbasics.object.accarea;
 
 import java.util.List;
 import java.util.Map;
 
 
Manages all available accounting areas.

Author(s):
Philip Helger
 
 public final class AccountingAreaManager extends AbstractSimpleDAO implements IAccountingAreaResolver
 {
   private static final String ELEMENT_ACCOUNTINGAREAS = "accountingareas";
   private static final String ELEMENT_ACCOUNTINGAREA = "accountingarea";
 
   private final Map <StringAccountingAream_aMap = new HashMap <StringAccountingArea> ();
 
   public AccountingAreaManager (@Nonnull @Nonempty final String sFilenamethrows DAOException
   {
     super (sFilename);
     initialRead ();
   }
 
   @Override
   @Nonnull
   protected EChange onRead (@Nonnull final IMicroDocument aDoc)
   {
     for (final IMicroElement eAccountingArea : aDoc.getDocumentElement ().getAllChildElements ())
       _addAccountingArea (MicroTypeConverter.convertToNative (eAccountingAreaAccountingArea.class));
     return .;
   }
 
   @Override
   @Nonnull
   protected IMicroDocument createWriteData ()
   {
     final IMicroDocument aDoc = new MicroDocument ();
     final IMicroElement eRoot = aDoc.appendElement ();
     for (final AccountingArea aAccountingArea : ContainerHelper.getSortedByKey ().values ())
       eRoot.appendChild (MicroTypeConverter.convertToMicroElement (aAccountingArea));
     return aDoc;
   }
 
   private void _addAccountingArea (@Nonnull final AccountingArea aAccountingArea)
   {
     ValueEnforcer.notNull (aAccountingArea"AccountingArea");
 
     final String sAccountingAreaID = aAccountingArea.getID ();
     if (.containsKey (sAccountingAreaID))
       throw new IllegalArgumentException ("AccountingArea ID '" + sAccountingAreaID + "' is already in use!");
     .put (aAccountingArea.getID (), aAccountingArea);
   }
 
   @Nonnull
   public IAccountingArea createAccountingArea (@Nonnull final IClient aClient,
                                                @Nonnull @Nonempty final String sDisplayName,
                                                @Nullable final String sCompanyType,
                                                @Nonnull @Nonempty final String sCompanyVATIN,
                                               @Nonnull @Nonempty final String sCompanyNumber,
                                               @Nonnull final IReadonlyAddress aAddress,
                                               @Nonnull final String sTelephone,
                                               @Nullable final String sFax,
                                               @Nullable final String sEmailAddress,
                                               @Nullable final String sWebSite,
                                               @Nonnull final ECurrency eDefaultCurrency,
                                               @Nullable final String sOfficeLocation,
                                               @Nullable final String sCommercialRegistrationNumber,
                                               @Nullable final String sCommercialCourt,
                                               @Nonnull final Locale aDisplayLocale)
  {
    final AccountingArea aAccountingArea = new AccountingArea (aClient,
                                                               sDisplayName,
                                                               sCompanyType,
                                                               sCompanyVATIN,
                                                               sCompanyNumber,
                                                               aAddress,
                                                               sTelephone,
                                                               sFax,
                                                               sEmailAddress,
                                                               sWebSite,
                                                               eDefaultCurrency,
                                                               sOfficeLocation,
                                                               sCommercialRegistrationNumber,
                                                               sCommercialCourt,
                                                               aDisplayLocale);
    .writeLock ().lock ();
    try
    {
      _addAccountingArea (aAccountingArea);
      markAsChanged ();
    }
    finally
    {
      .writeLock ().unlock ();
    }
                                     aAccountingArea.getID (),
                                     sDisplayName,
                                     sCompanyType,
                                     sCompanyVATIN,
                                     sCompanyNumber,
                                     aAddress,
                                     sTelephone,
                                     sFax,
                                     sEmailAddress,
                                     eDefaultCurrency,
                                     sOfficeLocation,
                                     sCommercialRegistrationNumber,
                                     sCommercialCourt);
    return aAccountingArea;
  }
  public EChange updateAccountingArea (@Nonnull @Nonempty final String sAccountingAreaID,
                                       @Nonnull @Nonempty final String sDisplayName,
                                       @Nullable final String sCompanyType,
                                       @Nonnull @Nonempty final String sCompanyVATIN,
                                       @Nonnull @Nonempty final String sCompanyNumber,
                                       @Nonnull final IReadonlyAddress aAddress,
                                       @Nonnull final String sTelephone,
                                       @Nullable final String sFax,
                                       @Nullable final String sEmailAddress,
                                       @Nullable final String sWebSite,
                                       @Nonnull final ECurrency eDefaultCurrency,
                                       @Nullable final String sOfficeLocation,
                                       @Nullable final String sCommercialRegistrationNumber,
                                       @Nullable final String sCommercialCourt,
                                       @Nonnull final Locale aDisplayLocale)
  {
    .writeLock ().lock ();
    try
    {
      final AccountingArea aAccountingArea = .get (sAccountingAreaID);
      if (aAccountingArea == null)
      {
        AuditUtils.onAuditModifyFailure (.sAccountingAreaID"no-such-id");
        return .;
      }
      EChange eChange = .;
      eChange = eChange.or (aAccountingArea.setDisplayName (sDisplayName));
      eChange = eChange.or (aAccountingArea.setCompanyType (sCompanyType));
      eChange = eChange.or (aAccountingArea.setCompanyVATIN (sCompanyVATIN));
      eChange = eChange.or (aAccountingArea.setCompanyNumber (sCompanyNumber));
      eChange = eChange.or (aAccountingArea.setAddress (aAddressaDisplayLocale));
      eChange = eChange.or (aAccountingArea.setTelephone (sTelephone));
      eChange = eChange.or (aAccountingArea.setFax (sFax));
      eChange = eChange.or (aAccountingArea.setEmailAddress (sEmailAddress));
      eChange = eChange.or (aAccountingArea.setWebSite (sWebSite));
      eChange = eChange.or (aAccountingArea.setDefaultCurrency (eDefaultCurrency));
      eChange = eChange.or (aAccountingArea.setOfficeLocation (sOfficeLocation));
      eChange = eChange.or (aAccountingArea.setCommercialRegistrationNumber (sCommercialRegistrationNumber));
      eChange = eChange.or (aAccountingArea.setCommercialCourt (sCommercialCourt));
      if (eChange.isUnchanged ())
        return .;
      aAccountingArea.setLastModificationNow ();
      markAsChanged ();
    }
    finally
    {
      .writeLock ().unlock ();
    }
                                     "all",
                                     sAccountingAreaID,
                                     sDisplayName,
                                     sCompanyType,
                                     sCompanyVATIN,
                                     sCompanyNumber,
                                     aAddress,
                                     sTelephone,
                                     sFax,
                                     sEmailAddress,
                                     eDefaultCurrency,
                                     sOfficeLocation,
                                     sCommercialRegistrationNumber,
                                     sCommercialCourt);
    return .;
  }
  public Collection <? extends IAccountingAreagetAllAccountingAreas ()
  {
    .readLock ().lock ();
    try
    {
      return ContainerHelper.newList (.values ());
    }
    finally
    {
      .readLock ().unlock ();
    }
  }
  public Collection <? extends IAccountingAreagetAllAccountingAreasOfClient (@Nullable final String sClientID)
  {
    final List <IAccountingArearet = new ArrayList <IAccountingArea> ();
    if (StringHelper.hasText (sClientID))
    {
      .readLock ().lock ();
      try
      {
        for (final IAccountingArea aAccountingArea : .values ())
          if (aAccountingArea.getClientID ().equals (sClientID))
            ret.add (aAccountingArea);
      }
      finally
      {
        .readLock ().unlock ();
      }
    }
    return ret;
  }
  public Collection <? extends IAccountingAreagetAllAccountingAreasOfClient (@Nullable final IClient aClient)
  {
    final List <IAccountingArearet = new ArrayList <IAccountingArea> ();
    if (aClient != null)
    {
      .readLock ().lock ();
      try
      {
        for (final IAccountingArea aAccountingArea : .values ())
          if (aAccountingArea.hasSameClient (aClient))
            ret.add (aAccountingArea);
      }
      finally
      {
        .readLock ().unlock ();
      }
    }
    return ret;
  }
  public Collection <StringgetAllAccountingAreaIDsOfClient (@Nullable final String sClientID)
  {
    final List <Stringret = new ArrayList <String> ();
    if (StringHelper.hasText (sClientID))
    {
      .readLock ().lock ();
      try
      {
        for (final IAccountingArea aAccountingArea : .values ())
          if (aAccountingArea.getClientID ().equals (sClientID))
            ret.add (aAccountingArea.getID ());
      }
      finally
      {
        .readLock ().unlock ();
      }
    }
    return ret;
  }
  {
    final List <Stringret = new ArrayList <String> ();
    if (aClient != null)
    {
      .readLock ().lock ();
      try
      {
        for (final IAccountingArea aAccountingArea : .values ())
          if (aAccountingArea.hasSameClient (aClient))
            ret.add (aAccountingArea.getID ());
      }
      finally
      {
        .readLock ().unlock ();
      }
    }
    return ret;
  }
  {
    if (StringHelper.hasNoText (sID))
      return null;
    .readLock ().lock ();
    try
    {
      return .get (sID);
    }
    finally
    {
      .readLock ().unlock ();
    }
  }
  public IAccountingArea getAccountingAreaOfID (@Nullable final String sID, @Nullable final IClient aClient)
  {
    final IAccountingArea aAccountingArea = getAccountingAreaOfID (sID);
    return aAccountingArea != null && aAccountingArea.hasSameClient (aClient) ? aAccountingArea : null;
  }
  public boolean containsAccountingAreaWithID (@Nullable final String sID)
  {
    if (StringHelper.hasNoText (sID))
      return false;
    .readLock ().lock ();
    try
    {
      return .containsKey (sID);
    }
    finally
    {
      .readLock ().unlock ();
    }
  }
  public boolean containsAccountingAreaWithID (@Nullable final String sID, @Nullable final IClient aClient)
  {
    return getAccountingAreaOfID (sIDaClient) != null;
  }
  public IAccountingArea getAccountingAreaOfName (@Nullable final String sName, @Nullable final IClient aClient)
  {
    if (StringHelper.hasText (sName) && aClient != null)
    {
      .readLock ().lock ();
      try
      {
        for (final IAccountingArea aAccountingArea : .values ())
          if (aAccountingArea.hasSameClient (aClient) && aAccountingArea.getDisplayName ().equals (sName))
            return aAccountingArea;
      }
      finally
      {
        .readLock ().unlock ();
      }
    }
    return null;
  }
New to GrepCode? Check out our FAQ X