All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.micromata.genome.logging.LogWriteEntry Maven / Gradle / Ivy

The newest version!
//
// Copyright (C) 2010-2016 Micromata GmbH
//
// 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 de.micromata.genome.logging;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Wrapper fuer einen Log-Eintrag.
 * 
 * @author [email protected]
 */
public class LogWriteEntry implements Serializable
{

  /**
   * The Constant serialVersionUID.
   */
  private static final long serialVersionUID = -5234050492871751919L;

  /**
   * if 0, use the db time to set timestamp.
   */
  private long timestamp;

  /**
   * The level.
   */
  private LogLevel level;

  /**
   * The category.
   */
  private String category;

  /**
   * The message.
   */
  private String message;

  /**
   * The attributes.
   */
  private List attributes;

  /**
   * After inserting an LogWriteEntry this value should be updated.
   * 
   * For database this will be probably the primary Key as Long
   */
  private Object logEntryIndex;

  public Object getLogEntryIndex()
  {
    return logEntryIndex;
  }

  public void setLogEntryIndex(Object logEntryIndex)
  {
    this.logEntryIndex = logEntryIndex;
  }

  /**
   * Instantiates a new log write entry.
   */
  public LogWriteEntry()
  {

  }

  /**
   * Instantiates a new log write entry.
   *
   * @param level the level
   * @param category the category
   * @param message the message
   * @param attributes the attributes
   */
  public LogWriteEntry(LogLevel level, String category, String message, List attributes)
  {
    this.level = level;
    this.category = category;
    this.message = message;
    this.attributes = attributes;
  }

  /**
   * As list.
   *
   * @param attributes the attributes
   * @return the list
   */
  private static List asList(LogAttribute[] attributes)
  {
    List ret = new ArrayList(attributes.length);
    for (int i = 0; i < attributes.length; ++i) {
      ret.add(attributes[i]);
    }
    return ret;
  }

  /**
   * Instantiates a new log write entry.
   *
   * @param level the level
   * @param category the category
   * @param message the message
   * @param attributes the attributes
   */
  public LogWriteEntry(LogLevel level, String category, String message, LogAttribute[] attributes)
  {
    this(level, category, message, asList(attributes));
  }

  /**
   * Push attribute.
   *
   * @param le the le
   */
  public void pushAttribute(LogAttribute le)
  {
    if (le == null) {
      return;
    }
    for (LogAttribute a : attributes) {
      if (a.getType().name().equals(le.getType().name()) == true) {
        return;
      }
    }
    attributes.add(le);
  }

  /**
   * Ensure unique attributes.
   */
  public void ensureUniqueAttributes()
  {
    Map tmp = new HashMap(attributes.size());
    for (LogAttribute la : attributes) {
      tmp.put(la.getTypeName(), la);
    }
    attributes.clear();
    attributes.addAll(tmp.values());
  }

  public List getAttributes()
  {
    return attributes;
  }

  /**
   * Gets the attribute by type.
   *
   * @param type the type
   * @return the attribute by type
   */
  public LogAttribute getAttributeByType(LogAttributeType type)
  {
    if (attributes == null) {
      return null;
    }
    if (type == null) {
      return null;
    }

    for (LogAttribute la : attributes) {
      if (la.getType().name().equals(type.name()) == true) {
        return la;
      }
    }
    return null;
  }

  @Override
  public String toString()
  {
    StringBuilder sb = new StringBuilder();
    sb.append(timestamp).append("|").append(level).append("|").append(message);
    if (attributes != null) {
      for (LogAttribute la : attributes) {
        sb.append(";").append(la.getTypeName()).append(": ").append(la.getValueToWrite(this));
      }
    }
    return sb.toString();
  }

  public void setAttributes(List attributes)
  {
    this.attributes = attributes;
  }

  public String getCategory()
  {
    return category;
  }

  public void setCategory(String category)
  {
    this.category = category;
  }

  public LogLevel getLevel()
  {
    return level;
  }

  public void setLevel(LogLevel level)
  {
    this.level = level;
  }

  public String getMessage()
  {
    return message;
  }

  public void setMessage(String message)
  {
    this.message = message;
  }

  public long getTimestamp()
  {
    return timestamp;
  }

  public void setTimestamp(long timestamp)
  {
    this.timestamp = timestamp;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy