2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.dmaap.dbcapi.model;
23 import java.lang.reflect.Field;
24 import java.util.ArrayList;
25 import java.util.Calendar;
26 import java.util.Date;
27 import java.util.TimeZone;
29 import javax.xml.bind.annotation.XmlRootElement;
31 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
34 public abstract class DmaapObject extends BaseLoggingClass {
35 protected Date lastMod;
36 protected DmaapObject_Status status;
38 public Date getLastMod() {
42 public void setLastMod(Date lastMod) {
43 this.lastMod = lastMod;
46 public void setLastMod() {
47 this.lastMod = Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTime();
50 public enum DmaapObject_Status {
58 public DmaapObject_Status getStatus() {
62 public void setStatus(DmaapObject_Status status) {
66 public void setStatus( String val ) {
67 if ( val == null || val.isEmpty() ) {
68 this.status = DmaapObject_Status.EMPTY;
69 } else if (val.compareToIgnoreCase("new") == 0 ) {
70 this.status = DmaapObject_Status.NEW;
71 } else if ( val.compareToIgnoreCase("staged" ) == 0) {
72 this.status = DmaapObject_Status.STAGED;
73 } else if ( val.compareToIgnoreCase("valid") == 0) {
74 this.status = DmaapObject_Status.VALID;
75 } else if ( val.compareToIgnoreCase("invalid") == 0) {
76 this.status = DmaapObject_Status.INVALID;
77 } else if ( val.compareToIgnoreCase("deleted") == 0) {
78 this.status = DmaapObject_Status.DELETED;
80 this.status = DmaapObject_Status.INVALID;
84 public boolean isStatusValid() {
85 if ( this.status == DmaapObject_Status.VALID ) {
92 * TODO: get this working so arrays and sub-class within an Object can be logged
94 public String toString() {
95 return classToString( this );
98 private String classToString( Object obj ) {
99 Field[] fields = obj.getClass().getDeclaredFields();
100 StringBuilder res = new StringBuilder( "{");
101 boolean first = true;
102 for ( Field field: fields ) {
103 logger.info( field.getName() + " toString=" + field.toString() + " toGenericString=" + field.toGenericString());
111 field.setAccessible(true); // avoid IllegalAccessException
114 Class<?> t = field.getType();
116 if ( t == String.class ) {
117 res.append( "\"" ).append( field.getName() ).append( "\": \"");
120 res.append(field.get(this));
121 } catch ( IllegalAccessException iae) {
122 res.append( "UNK(iae)");
123 } catch (IllegalArgumentException iae2 ) {
124 res.append( "UNK(iae2)");
125 } catch ( NullPointerException npe ) {
126 res.append( "UNK(npe)");
127 } catch ( ExceptionInInitializerError eie ) {
128 res.append( "UNK(eie)");
131 } else if ( t == ArrayList.class ){
133 res.append( classToString( field ));
139 return( res.toString());