Duplicate entry '0' for key 'PRIMARY' while adding data to MYSQL([JDBCExceptionReporter:76]) - liferay

getting following exception while inserting 2nd time in database from my liferay portlet.
[JDBCExceptionReporter:76] Duplicate entry '0' for key 'PRIMARY'.(i think its because my primary key value not getting auto increment)
I think have done mistake while auto incrementing the primary key in my custom portlet .but i don't know where i have to make changes for that.
if anyone can guide me about to where make the changes to solve this auto increment issue?
this is the code from auto increment been set
try {
restVar = restaurantPersistence.create(counterLocalService
.increment(restaurant.class.toString()));
} catch (SystemException e) {
e.printStackTrace();
return restVar = null;
}
try {
resourceLocalService.addResources(0,restParam.getGroupId(), restParam.getUserId(),
restaurant.class.getName(),restParam.getPrimaryKey(), false,true,true);
} catch (PortalException e) {
e.printStackTrace();
return restVar = null;
} catch (SystemException e) {
e.printStackTrace();
return restVar = null;
}

Try this one..
long primaryKeyId = CounterLocalServiceUtil.increment(ClassName.class.getName());
XYZDetails XYZDetails = XYZDetailsLocalServiceUtil.createXYZDetails(primaryKeyId);
Add other details using XYZDetails Obj
e.g
XYZDetails.setName("Name");
Then Save the Details..
XYZDetailsLocalServiceUtil.addXYZDetails(XYZDetails);
Hope this may help you !!!

Related

Marker is not displayed on HERE Map

I want a Marker to be displayed on the Map, but it's not displayed, here's the code I used,
I'm using HERE Map SDK for Android
Image img = new Image();
try {
img.setImageResource(R.drawable.marker);
} catch (IOException e) {
e.printStackTrace();
}
MapMarker mm = new MapMarker();
mm.setIcon(img);
mm.setCoordinate(new GeoCoordinate(21.609512, 39.131269));
After creating the MapMarker, you need to add it also to your map via Map.addMapObject(...)
See my example (where mMap is my instance of Map, and the anchor point is on the botton instead of center):
private void addMarker(GeoCoordinate geoCoordinate)
{
if (mMarker == null) {
Image image = new Image();
try {
image.setImageResource(R.drawable.pin);
} catch (final IOException e) {
e.printStackTrace();
}
mMarker = new MapMarker(geoCoordinate, image);
mMarker.setAnchorPoint(new PointF(image.getWidth()/2, image.getHeight()));
mMap.addMapObject(mMarker);
} else {
mMarker.setCoordinate(geoCoordinate);
}
mMap.setCenter(geoCoordinate, Animation.BOW);
}

OpenIMAJ - error reading feature list saved as ascii

Working with OpenIMAJ I'd like to save feature lists for later use but I'm getting a java.util.NoSuchElementException: No line found exception (see below) while re-reading the feature file I just saved. I've checked that the text file exists though I'm not really sure whether the full contents is what is ought to be (it's very long).
Any ideas what's wrong?
Thanks in advance!
(My trial code is pasted below).
java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at org.openimaj.image.feature.local.keypoints.Keypoint.readASCII(Keypoint.java:296)
at org.openimaj.feature.local.list.LocalFeatureListUtils.readASCII(LocalFeatureListUtils.java:170)
at org.openimaj.feature.local.list.LocalFeatureListUtils.readASCII(LocalFeatureListUtils.java:136)
at org.openimaj.feature.local.list.MemoryLocalFeatureList.read(MemoryLocalFeatureList.java:134)
...
My trial code looks like this:
Video<MBFImage> originalVideo = getVideo();
MBFImage frame = originalVideo.getCurrentFrame().clone();
DoGSIFTEngine engine = new DoGSIFTEngine();
LocalFeatureList<Keypoint> originalFeatureList = engine.findFeatures(frame.flatten());
try {
originalFeatureList.writeASCII(new PrintWriter(new File("featureList.txt")));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Saved feature list with "+originalFeatureList.size()+" keypoints.");
MemoryLocalFeatureList<Keypoint> loadedFeatureList = null;
try {
loadedFeatureList = MemoryLocalFeatureList.read(new File("featureList.txt"), Keypoint.class);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Loaded feature list with "+loadedFeatureList.size()+" keypoints.");
I think the problem is that you're not closing the PrintWriter used to save the features, and that it hasn't had a time to actually write the contents. However you shouldn't really use the LocalFeatureList.writeASCII method directly as it will not write the header information; rather use IOUtils.writeASCII. Replace:
originalFeatureList.writeASCII(new PrintWriter(new File("featureList.txt")));
with
IOUtils.writeASCII(new File("featureList.txt"), originalFeatureList);
and then it should work. This also deals with closing the file once it's written.

handling file not found exception?

I made this code to empty some files that I regularly delete, such as temp files in Windows. Several friends may wish to use the same application and I am working on the best way to handle the file not found exception.
How can this best be handled for use by multiple users?
public void Deletefiles()
{
try
{
string[] DirectoryList = Directory.GetDirectories("C:\\Users\\user\\Desktop\\1");
string[] FileList = Directory.GetFiles("C:\\Users\\user\\Desktop\\1");
foreach (string x in DirectoryList)
{
Directory.Delete(x, true);
FoldersCounter++;
}
foreach (string y in FileList)
{
File.Delete(y);
FilesCounter++;
}
MessageBox.Show("Done...\nFiles deleted - " + FileList.Length + "\nDirectories deleted - " + DirectoryList.Length + "\n" + FilesCounter + "\n", "message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception z)
{
if (z.Message.Contains("NotFound"))
{
MessageBox.Show("File Not Found");
}
else
{
throw (z);
}
//throw new FileNotFoundException();
}
}
Modifying you code as little as possible, you could simply wrap your Delete calls in a try/catch:
foreach (string x in DirectoryList)
{
try {
Directory.Delete(x, true);
}
catch (DirectoryNotFoundException e)
{
// do something, or not...
}
FoldersCounter++;
}
foreach (string y in FileList)
{
try
{
File.Delete(y);
}
catch (FileNotFoundException e)
{
// do something, or not...
}
FilesCounter++;
}
Remove the top level try/catch and just let the foreach statements cycle through -- trying and catching any exceptions as they come.
You don't necessarily need to alert the user that the file wasn't found. It is there it is going to be deleted, so the fact that it isn't there doesn't really effect the outcome of the program.
This isn't the most resource friendly method, but it is a simple enough application to not cause an issue.

Apache-thrift call to cassandra inserts junk/null values in the "key"

cassandra-thrift-1.1.2.jar
Problem code:
ColumnOrSuperColumn cosc = null;
org.apache.cassandra.thrift.Column c = new org.apache.cassandra.thrift.Column ();
c.setName ("full_name".getBytes ("UTF-8"));
c.setValue ("Test name".getBytes ("UTF-8"));
c.setTimestamp (System.currentTimeMillis());
// insert data
// long timestamp = System.currentTimeMillis();
try {
client.set_keyspace("CClient");
bb=ByteBuffer.allocate (10);
client.insert (bb.putInt(1),
new ColumnParent ("users"),
c,
ConsistencyLevel.QUORUM);
bb.putInt (2);
cosc = client.get (bb, cp, ConsistencyLevel.QUORUM);
}
catch (TimedOutException toe) {
System.out.println (toe.getMessage());
}
catch (org.apache.cassandra.thrift.UnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
System.out.println (new String (cosc.getColumn().getName()) + "-" + new String (cosc.getColumn().getValue()));
}
The code shown above inserts some junk or null into the database, I don't understand the reason why?
See how it looks on the CLI:
RowKey:
=> (column=full_name, value=Test name, timestamp=1345743985973)
Any help in this is greatly appreciated.
Thanks.
You're creating a row with row key as bytes.
In Cassandra cli you'll probably see the row key if you list the rows as bytes.
E.g. in cassandra cli type:
assume users keys as bytes;
list users;

Is it possible to use IMAP Query Terms in Javamail with GMail?

I am trying to programmatically retrieve the Call Log messages that are backup up from my android phone by a little application called SMSBackup (highly recommended).
What I want to do is to be able to retrieve the call logs for a particular day. I have tried the following program, using JavaMail:
public List<CallLogEntry> getCallLog(String username, String password, Date date, TimeZone tz) {
Store store = null;
try {
store = MailUtils.getGmailImapStore(username, password);
Folder folder = store.getDefaultFolder();
if (folder == null)
throw new Exception("No default folder");
Folder inboxfolder = folder.getFolder("Call log");
if (inboxfolder == null)
throw new Exception("No INBOX");
inboxfolder.open(Folder.READ_ONLY);
Date fromMidnight = new Date(TimeUtils.fromMidnight(date.getTime(), tz));
Date toMidnight = new Date(TimeUtils.toMidnight(date.getTime(), 0, tz));
SentDateTerm fromTerm = new SentDateTerm(SentDateTerm.GT, fromMidnight);
SentDateTerm toTerm = new SentDateTerm(SentDateTerm.LT, toMidnight);
AndTerm searchTerms = new AndTerm(fromTerm, toTerm);
Message[] msgs = inboxfolder.search(searchTerms);
FetchProfile fp = new FetchProfile();
fp.add("Subject");
fp.add("Content");
fp.add("From");
fp.add("SentDate");
inboxfolder.fetch(msgs, fp);
List<CallLogEntry> callLog = new ArrayList<CallLogEntry>();
for (Message message : msgs) {
CallLogEntry entry = new CallLogEntry();
entry.subject = message.getSubject();
entry.body = (String) message.getContent();
callLog.add(entry);
}
inboxfolder.close(false);
store.close();
return callLog;
} catch (NoSuchProviderException ex) {
ex.printStackTrace();
} catch (MessagingException ex) {
ex.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (store != null)
store.close();
} catch (MessagingException ex) {
ex.printStackTrace();
}
}
return null;
}
My two utility methods (fromMidnight / toMidnight):
public static final long fromMidnight(long time, TimeZone tz) {
Calendar c = Calendar.getInstance(tz);
c.setTimeInMillis(time);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 1);
return c.getTimeInMillis();
}
public static final long toMidnight(long time, int nDays, TimeZone tz) {
Calendar c = Calendar.getInstance(tz);
c.setTimeInMillis(time + nDays*MILLIS_IN_DAY);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 59);
c.set(Calendar.SECOND, 59);
c.set(Calendar.MILLISECOND, 999);
return c.getTimeInMillis();
}
However, for some reason:
while eventually executing, it takes about 3 minutes to complete
I'm getting back the entire Call log, i.e. the entire content of the "Call Log" folder in my mailbox
What am I missing?
The main thing that you're missing is that the underlying IMAP SEARCH syntax supports only dates, not date-times. So your query will result in JavaMail issuing the command:
A001 SEARCH SENTBEFORE 16-JAN-2011 SENTSINCE 16-JAN-2011 ALL
(Put a breakpoint in IMAPProtocol.issueSearch() to see this.)
GMail appears to freak out on this query, which logically cannot match any messages. Try switching your logic to a single term using SentDateTerm.EQ (which maps to SENTON) and it should work:
SentDateTerm term = new SentDateTerm(SentDateTerm.EQ, date.getTime());
Message[] msgs = inboxfolder.search(term);

Resources