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.migration;
 
 import java.util.List;
 import java.util.Set;
 
 
 
 
 public class SystemMigrationManager extends AbstractSimpleDAO
 {
   public static final ObjectType OT_SYSTEM_MIGRATION_RESULT = new ObjectType ("systemmigrationresult");
 
   private static final Logger s_aLogger = LoggerFactory.getLogger (SystemMigrationManager.class);
   private static final String ELEMENT_SYSTEM_MIGRATION_RESULTS = "systemmigrationresults";
   private static final String ELEMENT_SYSTEM_MIGRATION_RESULT = "systemmigrationresult";
 
 
   public SystemMigrationManager (@Nullable final String sFilenamethrows DAOException
   {
     super (sFilename);
     initialRead ();
   }
 
   @Override
   protected EChange onRead (final IMicroDocument aDoc)
   {
     for (final IMicroElement eMigrationResult : aDoc.getDocumentElement ()
                                                     .getAllChildElements ())
       internalAdd (MicroTypeConverter.convertToNative (eMigrationResultSystemMigrationResult.class));
     return .;
   }
 
   @Override
   protected IMicroDocument createWriteData ()
   {
     final IMicroDocument aDoc = new MicroDocument ();
     for (final List <SystemMigrationResultaMigrationResults : ContainerHelper.getSortedByKey ().values ())
       for (final SystemMigrationResult aMigrationResult : aMigrationResults)
         eRoot.appendChild (MicroTypeConverter.convertToMicroElement (aMigrationResult));
     return aDoc;
   }
 
   void internalAdd (@Nonnull final SystemMigrationResult aMigrationResult)
   {
     ValueEnforcer.notNull (aMigrationResult"MigrationResult");
 
     final String sMigrationID = aMigrationResult.getID ();
     .putSingle (sMigrationIDaMigrationResult);
   }
 
   public void addMigrationResult (@Nonnull final SystemMigrationResult aMigrationResult)
   {
     ValueEnforcer.notNull (aMigrationResult"MigrationResult");
 
     .writeLock ().lock ();
    try
    {
      internalAdd (aMigrationResult);
      markAsChanged ();
    }
    finally
    {
      .writeLock ().unlock ();
    }
                                     aMigrationResult.getID (),
                                     Boolean.valueOf (aMigrationResult.isSuccess ()),
                                     aMigrationResult.getErrorMessage ());
  }

  
Mark the specified migration as success.

Parameters:
sMigrationID The migration ID to be added. May neither be null nor empty.
  public void addMigrationResultSuccess (@Nonnull @Nonempty final String sMigrationID)
  {
    addMigrationResult (SystemMigrationResult.createSuccess (sMigrationID));
  }

  
Mark the specified migration as failed.

Parameters:
sMigrationID The migration ID to be added. May neither be null nor empty.
sErrorMsg The error message. May not be null.
  public void addMigrationResultError (@Nonnull @Nonempty final String sMigrationID, @Nonnull final String sErrorMsg)
  {
    addMigrationResult (SystemMigrationResult.createFailure (sMigrationIDsErrorMsg));
  }
  public List <SystemMigrationResultgetAllMigrationResults (@Nullable final String sMigrationID)
  {
    .readLock ().lock ();
    try
    {
      return ContainerHelper.newList (.get (sMigrationID));
    }
    finally
    {
      .readLock ().unlock ();
    }
  }
  {
    final List <SystemMigrationResultret = new ArrayList <SystemMigrationResult> ();
    .readLock ().lock ();
    try
    {
      for (final List <SystemMigrationResultaResults : .values ())
        ret.addAll (aResults);
    }
    finally
    {
      .readLock ().unlock ();
    }
    return ret;
  }
  {
    final List <SystemMigrationResultret = new ArrayList <SystemMigrationResult> ();
    for (final SystemMigrationResult aMigrationResult : getAllMigrationResults (sMigrationID))
      if (aMigrationResult.isFailure ())
        ret.add (aMigrationResult);
    return ret;
  }
  public boolean wasMigrationExecutedSuccessfully (@Nullable final String sMigrationID)
  {
    final List <SystemMigrationResultaResults = getAllMigrationResults (sMigrationID);
    for (final SystemMigrationResult aMigrationResult : aResults)
      if (aMigrationResult.isSuccess ())
        return true;
    return false;
  }
  public Set <StringgetAllMigrationIDs ()
  {
    .readLock ().lock ();
    try
    {
      return ContainerHelper.newSet (.keySet ());
    }
    finally
    {
      .readLock ().unlock ();
    }
  }

  
Perform a migration if it was not performed yet. The performed callback may not throw an error or return an error. All migrations executed with this method will be handled as a success only.

Parameters:
sMigrationID The migration ID to handle. May neither be null nor empty.
aMigrationAction The action to be performed. May not be null.
  public void performMigrationIfNecessary (@Nonnull @Nonempty final String sMigrationID,
                                           @Nonnull final INonThrowingRunnable aMigrationAction)
  {
    ValueEnforcer.notEmpty (sMigrationID"MigrationID");
    ValueEnforcer.notNull (aMigrationAction"MigrationAction");
    if (!wasMigrationExecutedSuccessfully (sMigrationID))
    {
      try
      {
        // Invoke the callback
        aMigrationAction.run ();
        // Always assume success
        addMigrationResultSuccess (sMigrationID);
      }
      catch (final RuntimeException ex)
      {
        .error ("Error execution system migration '" + sMigrationID + "'"ex);
        addMigrationResultError (sMigrationIDex.getClass () + ": " + ex.getMessage ());
      }
    }
  }

  
Perform a migration if it was not performed yet.

Parameters:
sMigrationID The migration ID to handle. May neither be null nor empty.
aMigrationAction The action to be performed. May not be null.
  public void performMigrationIfNecessary (@Nonnull @Nonempty final String sMigrationID,
                                           @Nonnull final INonThrowingCallable <SuccessWithValue <String>> aMigrationAction)
  {
    ValueEnforcer.notEmpty (sMigrationID"MigrationID");
    ValueEnforcer.notNull (aMigrationAction"MigrationAction");
    if (!wasMigrationExecutedSuccessfully (sMigrationID))
    {
      try
      {
        // Invoke the callback
        final SuccessWithValue <Stringret = aMigrationAction.call ();
        // Success or error
        if (ret.isSuccess ())
          addMigrationResultSuccess (sMigrationID);
        else
          addMigrationResultError (sMigrationIDret.get ());
      }
      catch (final RuntimeException ex)
      {
        .error ("Error execution system migration '" + sMigrationID + "'"ex);
        addMigrationResultError (sMigrationIDex.getClass () + ": " + ex.getMessage ());
      }
    }
  }
  public String toString ()
  {
    return new ToStringGenerator (this).append ("map").toString ();
  }
New to GrepCode? Check out our FAQ X