Friday, August 24, 2018

Using Hibernate query : colon gets treated as parameter / escaping colon

return sessionFactory.getCurrentSession().
            createQuery("FROM Weather WHERE city_id = :id AND date " +
                    "BETWEEN now()::date AND now()::date + (:days - 1)").
                    setInteger("id", city_id).setString("days", days).list();

getting error:

org.hibernate.hql.ast.QuerySyntaxException: unexpected token: :

How can I use this syntax in HQL?

Basically the problem is that I want to use colon(:) in my query, but when hibernate sees colon, it thinks that it is a paramter(:parameterName is syntax for parameters in HQL), as you can see from my 2 uses(:id and :days).

But when I am using now()::date statement, it is specific postgreSQL syntax, hibernate ruins everything.

Solved

Since you're on Postgres, I would change the date() completely:

return sessionFactory.getCurrentSession().
        createQuery("FROM Weather WHERE city_id = :id AND date " +
                "BETWEEN current_date AND (current_date + (integer :days - 1))").
                setInteger("id", city_id).setString("days", days).list();

See http://www.postgresql.org/docs/8.2/static/functions-datetime.html


I just had this problem, had to use casts, so I tried some stuff to make it work. Turns out you escape : in hibernate with \

However, in java, to print \ to begin with, you have to escape it with \.

So, if you want to put a : in your SQL hibernate query, you have to write it like: \\:

And if you wanted to cast in PostgreSQL, such as in my case, you would have to, for example: field\\:\\:int if you wanted to cast some field as an integer.


Take a look at http://www.postgresql.org/docs/8.1/static/sql-createcast.html

Try using cast. To me it worked like a charm.


return sessionFactory.getCurrentSession().
        createQuery("FROM Weather WHERE city_id = :id AND date " +
                "BETWEEN cast(now() as date) AND cast(now() as date) + (:days - 1)").
                setInteger("id", city_id).setString("days", days).list();

Named parameters take a colon ':' like this Is that what you were looking for ?


You escape : with ::. I think.

Or try a nativequery


Monday, August 20, 2018

framework7 vuejs clicking on back button loses the content?

I am sure there's a configuration setting somewhere, but not sure where it is at?

I have app page that loads content from axios methods into a listview. Then, I click on the item list and it goes to the next page fine. When I clicked back, the listview loses all the items from the axios request. Is there a way to keep the content within listview without calling the axios methods again?


Sunday, August 19, 2018

How to generate PDF from XML message

What is the best practice to generate PDF out of the current message.

I'm thinking about using xsl-fo but I didn't find any components for that.

Any suggestions ?

Solved

Creating a custom mediator

https://docs.wso2.com/display/ESB500/Sample+380%3A+Writing+your+own+Custom+Mediation+in+Java

You can see here an example

https://github.com/Mystes/wso2-esb-pdf-mediator/blob/master/README.md


Saturday, August 18, 2018

Divide a .wav file according to the video frame rate in Matlab

I have a yuv video (say stream.yuv) and the corresponding audio file (stream.wav). Now I have GUI which renders the raw video on a frame by frame basis. The problem I am currently facing is that I need to play the audio associated with each video frame.

I've tried the following to begin with, but when I execute this, the audio sounds chop

%% Dividing the audio into per-second samples
nframes = 720;
[audioFile, audioSampleFreq] = audioread('stream.wav');
numSamples = length(audioFile); 
audioLength = round(numSamples / audioSampleFreq);

for frame = 1:audioLength
    start = (frame-1)*audioSampleFreq+1;
    stop = frame*audioSampleFreq; 
    [start stop]
    audioFrame1 = audioFile(start:stop,:);
    sound(audioFrame1,audioSampleFreq);
end

To divide the audio into samples per video frame,

    audioFileName = 'stream.wav';
    nframes = 720;
    framerate = 25;
    [audioFile, audioSampleFreq] = audioread(audioFileName);

    audioFRate = round(audioSampleFreq/framerate);

    %% Total number of audio samples
    numSamples = length(audioFile); 

    %% number of audio frames
    numFrames = floor(numSamples/audioFRate);  

    for frame = 1:numFrames
        start = (frame-1)*audioFRate+1;
        stop = frame*audioFRate; 
        [start stop]
        audioFrame1 = audioFile(start:stop,:);
        sound(audioFrame1,audioSampleFreq);   
    end

Any thoughts on how to synchronize the per-frame rendering of the YUV frames and the per-second audio samples? Thanks!

Solved

I think you approached the problem the wrong way round. Adapting your audio signal to the video signal is typically the wrong way round because small disruptions in a audio stream are very noticeable, while the average viewer does not notice missing frames. Instead, synchronize your video signal to the audio signal. To get a quick start, set up a audio player and start with some code like this:

videorate=25 %fps
audiorate=audio.SampleRate %sample rate
while audio.isplaying()
   displayFrame(ceil(audio.CurrentSample/audiorate*videorate))
end

There is much which can be improved but I assume this simple implementation will already show better results than your current attempt.

Things to improve

  • Don't render the same frame twice.
  • The ceil is just a quick and dirty approximation. Instead you need to call your displayFrame for frame f at time f/fps-processing_delay where processing_delay is the time between calling displayFrame and the image actually appearing on the screen. Set it to 1/25 initially and tweak it manually if audio and video seems to be not synchronized.
  • When missing the time where you should call displayFrame make a decision. For small times still display frame, for larger gaps drop the frame and continue with the next one. Maybe pause to display not to early.
  • When your Video signal really messes up, e.g. dropping 15 out of the last 25 frames, check for possible causes. Processing high resolution raw video, this typically happens because your HDD is to slow, so pausing both video and audio for a short time should allow the buffers to fill up again.

An alternative to this approach are approaches using variable playback speed, slightly adjusting the audio and video playback speed to get both synchronized. I assume that such approaches won't succeed in Matlab because you need to process your data with very low latencies to get this approach done.