001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.db.log;
029
030import org.opencms.db.CmsDbContext;
031import org.opencms.util.CmsUUID;
032
033import java.util.Arrays;
034import java.util.Date;
035import java.util.Locale;
036
037/**
038 * Wrapper class for a DB log entry.<p>
039 *
040 * @since 8.0.0
041 */
042public class CmsLogEntry {
043
044    /** DB context attribute name constant. */
045    public static final String ATTR_LOG_ENTRY = "ATTR_LOG_ENTRY";
046
047    /** The additional data. */
048    private final String[] m_data;
049
050    /** The entry's date in milliseconds. */
051    private final long m_date;
052
053    /** The structure id. */
054    private final CmsUUID m_structureId;
055
056    /** The type. */
057    private final CmsLogEntryType m_type;
058
059    /** The user id. */
060    private final CmsUUID m_userId;
061
062    /**
063     * Public constructor, will use the current time for time stamp.<p>
064     *
065     * @param dbc the current database context with the current user
066     * @param structureId the structure id
067     * @param type the entry type
068     * @param data the additional data to be parsed as a map
069     */
070    public CmsLogEntry(CmsDbContext dbc, CmsUUID structureId, CmsLogEntryType type, String[] data) {
071
072        m_userId = dbc.currentUser().getId();
073        m_date = System.currentTimeMillis();
074        m_structureId = structureId;
075        m_data = (data == null ? new String[0] : data);
076        m_type = type;
077    }
078
079    /**
080     * Public constructor.<p>
081     *
082     * @param userId the user id
083     * @param date the date in milliseconds
084     * @param structureId the structure id
085     * @param type the entry type
086     * @param data the optional additional data
087     */
088    public CmsLogEntry(CmsUUID userId, long date, CmsUUID structureId, CmsLogEntryType type, String[] data) {
089
090        m_userId = userId;
091        m_date = date;
092        m_structureId = structureId != null ? structureId : CmsUUID.getNullUUID();
093        m_data = (data == null ? new String[0] : data);
094        m_type = type;
095    }
096
097    /**
098     * Returns the additional data.<p>
099     *
100     * @return the additional data
101     */
102    public String[] getData() {
103
104        return m_data;
105    }
106
107    /**
108     * Returns the date.<p>
109     *
110     * @return the date
111     */
112    public long getDate() {
113
114        return m_date;
115    }
116
117    /**
118     * Returns the formatted details for this log entry.<p>
119     *
120     * @param locale the locale
121     *
122     * @return the formatted details for this log entry
123     */
124    public String getDetails(Locale locale) {
125
126        return Messages.get().getBundle(locale).key(m_type.getDetailKey(), m_data);
127    }
128
129    /**
130     * Returns the structure id.<p>
131     *
132     * @return the structure id
133     */
134    public CmsUUID getStructureId() {
135
136        return m_structureId;
137    }
138
139    /**
140     * Returns the type.<p>
141     *
142     * @return the type
143     */
144    public CmsLogEntryType getType() {
145
146        return m_type;
147    }
148
149    /**
150     * Returns the user id.<p>
151     *
152     * @return the user id
153     */
154    public CmsUUID getUserId() {
155
156        return m_userId;
157    }
158
159    /**
160     * @see java.lang.Object#toString()
161     */
162    @Override
163    public String toString() {
164
165        StringBuffer sb = new StringBuffer();
166        sb.append("[").append(getClass().getName()).append(":");
167        sb.append("user=").append(m_userId).append(",");
168        sb.append("date=").append(new Date(m_date)).append(",");
169        sb.append("structure=").append(m_structureId).append(",");
170        sb.append("type=").append(m_type.getLocalizedName(Locale.ENGLISH)).append(",");
171        sb.append("data=").append(Arrays.toString(m_data)).append(",");
172        return sb.append("]").toString();
173    }
174}