From dc76ebe217f5e1bcce5dc03f413e8b3f5cd83d1e Mon Sep 17 00:00:00 2001 From: Walter Medvedeo Date: Wed, 21 Oct 2015 15:07:32 +0200 Subject: [PATCH] BZ-1211257 - Comments always show the 'current' time (cherry picked from commit 7a51dcdf7468363426f68ec773589ec1b54f3b65) --- .../discussion/CommentLinePresenter.java | 53 ++++++++++++ .../client/discussion/CommentLineView.java | 36 +++++++++ ...mentLine.java => CommentLineViewImpl.java} | 51 ++++++------ ...Line.ui.xml => CommentLineViewImpl.ui.xml} | 0 .../discussion/DiscussionWidgetViewImpl.java | 5 +- .../widgets/client/util/DateFormatHelper.java | 33 ++++++++ .../discussion/CommentLinePresenterTest.java | 81 +++++++++++++++++++ 7 files changed, 234 insertions(+), 25 deletions(-) create mode 100644 kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/discussion/CommentLinePresenter.java create mode 100644 kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/discussion/CommentLineView.java rename kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/discussion/{CommentLine.java => CommentLineViewImpl.java} (52%) rename kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/discussion/{CommentLine.ui.xml => CommentLineViewImpl.ui.xml} (100%) create mode 100644 kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/util/DateFormatHelper.java create mode 100644 kie-wb-common-widgets/kie-wb-common-ui/src/test/java/org/kie/workbench/common/widgets/client/discussion/CommentLinePresenterTest.java diff --git a/kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/discussion/CommentLinePresenter.java b/kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/discussion/CommentLinePresenter.java new file mode 100644 index 00000000000..e9fd16a9711 --- /dev/null +++ b/kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/discussion/CommentLinePresenter.java @@ -0,0 +1,53 @@ +/* + * Copyright 2014 JBoss Inc + * + * 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 org.kie.workbench.common.widgets.client.discussion; + +import com.google.gwt.user.client.ui.IsWidget; +import com.google.gwt.user.client.ui.Widget; +import org.guvnor.common.services.shared.metadata.model.DiscussionRecord; +import org.kie.workbench.common.widgets.client.util.DateFormatHelper; + +public class CommentLinePresenter + implements IsWidget, + CommentLineView.Presenter { + + private CommentLineView view; + + public CommentLinePresenter() { + this( new CommentLineViewImpl() ); + } + + public CommentLinePresenter( CommentLineView view ) { + this.view = view; + view.setPresenter( this ); + } + + protected String formatTimestamp( long timestamp ) { + return DateFormatHelper.shortFormat( timestamp ); + } + + @Override + public Widget asWidget() { + return view.asWidget(); + } + + public void setRecord( DiscussionRecord record ) { + view.setAuthor( record.getAuthor() + ":" ); + view.setComment( "\"" + record.getNote() + "\"" ); + view.setDate( formatTimestamp( record.getTimestamp() ) ); + } +} \ No newline at end of file diff --git a/kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/discussion/CommentLineView.java b/kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/discussion/CommentLineView.java new file mode 100644 index 00000000000..192ecda684c --- /dev/null +++ b/kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/discussion/CommentLineView.java @@ -0,0 +1,36 @@ +/* + * Copyright 2015 JBoss Inc + * + * 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 org.kie.workbench.common.widgets.client.discussion; + +import com.google.gwt.user.client.ui.IsWidget; + +public interface CommentLineView + extends IsWidget { + + interface Presenter { + + } + + void setPresenter( Presenter presenter ); + + void setAuthor( String author ); + + void setComment( String comment ); + + void setDate( String date ); + +} \ No newline at end of file diff --git a/kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/discussion/CommentLine.java b/kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/discussion/CommentLineViewImpl.java similarity index 52% rename from kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/discussion/CommentLine.java rename to kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/discussion/CommentLineViewImpl.java index f594b097579..59c16dc1f25 100644 --- a/kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/discussion/CommentLine.java +++ b/kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/discussion/CommentLineViewImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 JBoss Inc + * Copyright 2015 JBoss Inc * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,33 +16,23 @@ package org.kie.workbench.common.widgets.client.discussion; -import java.util.Date; - import com.google.gwt.core.client.GWT; -import com.google.gwt.i18n.client.DateTimeFormat; import com.google.gwt.uibinder.client.UiBinder; import com.google.gwt.uibinder.client.UiField; import com.google.gwt.user.client.ui.Composite; -import com.google.gwt.user.client.ui.FocusPanel; -import com.google.gwt.user.client.ui.IsWidget; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.Widget; -import org.guvnor.common.services.shared.metadata.model.DiscussionRecord; -public class CommentLine - extends Composite - implements IsWidget { +public class CommentLineViewImpl extends Composite + implements CommentLineView { interface Binder extends - UiBinder { + UiBinder { } - private static Binder uiBinder = GWT.create(Binder.class); - - @UiField - FocusPanel base; + private static Binder uiBinder = GWT.create( Binder.class ); @UiField Label author; @@ -53,14 +43,29 @@ interface Binder @UiField Label comment; - public CommentLine(DiscussionRecord record) { - initWidget(uiBinder.createAndBindUi(this)); + private Presenter presenter; + + public CommentLineViewImpl( ) { + initWidget( uiBinder.createAndBindUi( this ) ); + } + + public void setPresenter( Presenter presenter ) { + this.presenter = presenter; + } + + @Override + public void setAuthor( String author ) { + this.author.setText( author ); + } - this.author.setText(record.getAuthor() + ":"); - this.comment.setText("\"" + record.getNote() + "\""); - this.date.setText( - DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.DATE_TIME_SHORT).format( -// new Date(record.getTimestamp()) - new Date())); + @Override + public void setComment( String comment ) { + this.comment.setText( comment ); + } + + @Override + public void setDate( String date ) { + this.date.setText( date ); } } + diff --git a/kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/discussion/CommentLine.ui.xml b/kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/discussion/CommentLineViewImpl.ui.xml similarity index 100% rename from kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/discussion/CommentLine.ui.xml rename to kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/discussion/CommentLineViewImpl.ui.xml diff --git a/kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/discussion/DiscussionWidgetViewImpl.java b/kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/discussion/DiscussionWidgetViewImpl.java index 1e9ec064302..c94d91d0f59 100644 --- a/kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/discussion/DiscussionWidgetViewImpl.java +++ b/kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/discussion/DiscussionWidgetViewImpl.java @@ -63,9 +63,10 @@ public void setPresenter( Presenter presenter ) { this.presenter = presenter; } - @Override public void addRow( DiscussionRecord line ) { - lines.add( new CommentLine( line ) ); + CommentLinePresenter commentLine = new CommentLinePresenter( ); + commentLine.setRecord( line ); + lines.add( commentLine ); } @Override diff --git a/kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/util/DateFormatHelper.java b/kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/util/DateFormatHelper.java new file mode 100644 index 00000000000..a59def9548b --- /dev/null +++ b/kie-wb-common-widgets/kie-wb-common-ui/src/main/java/org/kie/workbench/common/widgets/client/util/DateFormatHelper.java @@ -0,0 +1,33 @@ +/* + * Copyright 2015 JBoss Inc + * + * 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 org.kie.workbench.common.widgets.client.util; + +import java.util.Date; + +import com.google.gwt.i18n.client.DateTimeFormat; + +public class DateFormatHelper { + + public static String shortFormat( long timestamp ) { + return format( timestamp, DateTimeFormat.PredefinedFormat.DATE_TIME_SHORT ); + } + + public static String format( long timestamp, DateTimeFormat.PredefinedFormat format ) { + return DateTimeFormat.getFormat( format ).format( + new Date( timestamp ) ); + } +} diff --git a/kie-wb-common-widgets/kie-wb-common-ui/src/test/java/org/kie/workbench/common/widgets/client/discussion/CommentLinePresenterTest.java b/kie-wb-common-widgets/kie-wb-common-ui/src/test/java/org/kie/workbench/common/widgets/client/discussion/CommentLinePresenterTest.java new file mode 100644 index 00000000000..abfe570822b --- /dev/null +++ b/kie-wb-common-widgets/kie-wb-common-ui/src/test/java/org/kie/workbench/common/widgets/client/discussion/CommentLinePresenterTest.java @@ -0,0 +1,81 @@ +/* + * Copyright 2015 JBoss Inc + * + * 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 org.kie.workbench.common.widgets.client.discussion; + +import java.text.SimpleDateFormat; +import java.util.Date; + +import org.guvnor.common.services.shared.metadata.model.DiscussionRecord; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +import static org.mockito.Mockito.*; + +@RunWith( MockitoJUnitRunner.class ) +public class CommentLinePresenterTest { + + @Mock + private CommentLineView view; + + @InjectMocks + CommentLinePresenter presenter; + + @Test + public void testVisualization() { + + CommentLineView view = mock( CommentLineView.class ); + CommentLinePresenter presenter = new CommentLinePresenterWithNOGWTCode( view ); + + Date commentDate = new Date( ); + DiscussionRecord record = new DiscussionRecord( commentDate.getTime(), "test user", "test note" ); + presenter.setRecord( record ); + + verify( view, times( 1 ) ).setAuthor( eq ( expectedAuthorFormat( "test user" ) ) ); + verify( view, times( 1 ) ).setComment( eq( expectedCommentFormat( "test note" ) ) ); + verify( view, times( 1 ) ).setDate( expectedDateFormat( commentDate.getTime() ) ); + } + + private String expectedDateFormat( long timestamp ) { + //expected format to see in the UI -> "2015-10-21 12:09" + SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm" ); + return sdf.format( new Date( timestamp ) ); + } + + private String expectedCommentFormat( String comment ) { + return "\"" + comment + "\""; + } + + private String expectedAuthorFormat( String author ) { + return author + ":"; + } + + private class CommentLinePresenterWithNOGWTCode extends CommentLinePresenter { + + public CommentLinePresenterWithNOGWTCode( CommentLineView view ) { + super( view ); + } + + @Override + protected String formatTimestamp( long timestamp ) { + //override method to avoid GWT invocation during test + return expectedDateFormat( timestamp ); + } + } +}