Replacing a string in Google Protocol Data Buffers - java

I've succesfully built the following .proto file:
package JervisStorage;
option java_package = "TextBase";
option java_outer_classname = "JervisStorage";
message Owner {
optional string name = 1;
optional string sex = 2;
optional string profession = 3;
optional string email = 4;
}
Now, I managed to build the Owner:
private static Owner owner;
private static FileOutputStream serialOutput;
Owner pusheen= Owner.newBuilder()
.setName("Siema")
.setSex(" ")
.setProfession(" ")
.setEmail(" ")
.build();
I wrote the object to the file and successfully read the object from the file:
serialOutput = new FileOutputStream("JervisStorage.ser");
pusheen.writeTo(serialOutput);
serialOutput.close();
owner = Owner.parseFrom(new FileInputStream("JervisStorage.ser"));
System.out.println(owner.getName());
The problem is that I am unable to replace a signle record, write it back to the file and read the whole updated object. I have been trying to do this:
owner.toBuilder().setName("newName").build();
System.out.println(owner.getName());
serialOutput = new FileOutputStream("JervisStorage.ser");
owner.writeTo(serialOutput);
serialOutput.close();
owner = Owner.parseFrom(new FileInputStream("JervisStorage.ser"));
System.out.println(owner.getName());
Unfortunately, this approach does not seem to work... Can anyone help?

why not do
editedOwner = Owner.newBuilder()
.mergeFrom(new FileInputStream("JervisStorage.ser"))
.setName("new name")
.build();
alternatively you could do
editedOwner = owner.toBuilder()
.setName("new name")
.build();

Ok, I managed to find a solution to this problem. I hate extending the computation time when unnecessary, but this is the best I can think of so far. I've got two Owner objects: owner and editedOwner:
Owner owner;//for reading the records from the file
Owner editedOwner;// for addidng to the file
owner = Owner.parseFrom(new FileInputStream("JervisStorage.ser"));
editedOwner = Owner.newBuilder()
.setName("new name")
.setSex(owner.getSex())
.setProfession(owner.getProfession())
.setEmail(owner.getEmail())
.build();
serialOutput = new FileOutputStream("JervisStorage.ser");
editedOwner.writeTo(serialOutput);
serialOutput.close();
Fortunately, this little messy approach solves my problem for now. Thank you.

Related

How to use UpdateEventSourceMappingRequest in java?

I'm trying to use something like this:
UpdateEventSourceMappingRequest request = new UpdateEventSourceMappingRequest()
.withFunctionName("arn:aws:lambda:us-east-1:9999999999:function:"+functionName)
.withEnabled(false);
But I received a error because I have to use .withUUID(uuid):
UpdateEventSourceMappingRequest request = new UpdateEventSourceMappingRequest()
.withUUID(uuid))
.withFunctionName("arn:aws:lambda:us-east-1:9999999999:function:"+functionName)
.withEnabled(false);
I don't know how to get the value of uuid ( uuid from aws lambda ).
Can you help me with the solution to my problem ?
You need to provide the UUID identifier of the event source mapping to update it (and this field is mandatory). Update-request is not intended to create it.
When you create an event source mapping (here) - aws should return a response with a UUID identifier which you then may use in the update request.
That's the solution that I founded:
String strUUID = "";
ListEventSourceMappingsRequest requestList = new ListEventSourceMappingsRequest()
.withEventSourceArn("arn:aws:sqs:us-east-1:9999999999:test");
ListEventSourceMappingsResult result = awsLambda.listEventSourceMappings(requestList);
List<EventSourceMappingConfiguration> eventSourceMappings = result.getEventSourceMappings();
for (EventSourceMappingConfiguration eventLambda : eventSourceMappings) {
strUUID = eventLambda.getUUID();
}
System.out.println("Output UUID " + strUUID);
We have to use the ARN of the SQS that's trigger of the aws lambda.

Creating a poll system using PircBot

I'm new to Java and I'm trying to create a poll system using PircBot.
So far my code is this:
if (message.startsWith("!poll")) {
String polly = message.substring(6);
String[] vote = polly.split(" ");
String vote1 = vote[0];
String vote2 = vote[1];
}
Which splits the strings so that someone can type !poll "option1 option2" for example and it will be split into vote1 = option1 and vote2 = option2.
I'm kind of lost from here. Am I even heading in the right direction for creating a voting system?
I figure that I'd have a separate statement as follows.
if (message.equalsIgnoreCase("!vote " + option1))
But I'm not sure where to go with that either.

Create Liferay public pages on server start

I would like to create a Liferay hook that creates public pages on server start.
I already have the hook bound to server start. But I have some problems with creating the page. Maybe I am wrong about user, group, community, etc. (remember that this hook doesn't have access to themeDisplay).
Company company = CompanyLocalServiceUtil.getCompanyByMx(PropsUtil.get(PropsKeys.COMPANY_DEFAULT_WEB_ID));
long companyId = company.getCompanyId();
User defaultUser = UserLocalServiceUtil.getDefaultUser(companyId);
long userId = defaultUser.getUserId();
long groupId = GroupLocalServiceUtil.getCompanyGroup(PortalUtil.getDefaultCompanyId()).getGroupId();
boolean privateLayout = false;
long parentLayoutId = LayoutConstants.DEFAULT_PARENT_LAYOUT_ID;
String name = page.getName();
String title = null;
String description = null;
String type = LayoutConstants.TYPE_PORTLET;
boolean hidden = false;
String friendlyUrl = "/test";
ServiceContext serviceContext = new ServiceContext();
serviceContext.setScopeGroupId(groupId);
LayoutLocalServiceUtil.addLayout(userId, groupId, privateLayout, parentLayoutId, name, title, description, type, hidden, friendlyUrl, serviceContext);
LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();
layoutTypePortlet.setLayoutTemplateId(userId, page.getLayoutId());
LayoutLocalServiceUtil.updateLayout(layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(), layout.getTypeSettings());
The page is never listed...
You might want to check the leftovers of that old sevencogs hook demo code - James Falkner has blogged about it and resurrected some of the code. It might not be for the current version, but if there are any API changes, they're minor.
Pay special attention to the "Adding a Layout (Page) to a Site" paragraph in that article and compare it with your code.
Also note that "on server start" means that you'll have to pay attention to not create the same page twice. It'd happen on every single server start - or actually on every deployment of the hook that you write.

list of vaadin's special path variables #JavaScript(value = { "vaadin://...", "???://..." }

I try to add js file to output html code in vaadin7.4.13+maven project.
I know that it is possible to use vaadin://... protocol which is translated to /VAADIN/ directory.
Now I know that this isn't the only one trick it can be used there because I've seen some other protocols being used in #JavaScript annotation but I cannot recall them. Please help me with this.
vaadin://
???://
???://
...
Looking at vaadin's source code of com.vaadin.shared.ApplicationConstants I found out these:
APP_PATH = "APP";
UIDL_PATH = "UIDL";
HEARTBEAT_PATH = "HEARTBEAT";
PUSH_PATH = "PUSH";
PUBLISHED_FILE_PATH = APP_PATH + '/' + "PUBLISHED";
APP_PROTOCOL_PREFIX = "app://";
VAADIN_PROTOCOL_PREFIX = "vaadin://";
FONTICON_PROTOCOL_PREFIX = "fonticon://";
PUBLISHED_PROTOCOL_NAME = "published";
PUBLISHED_PROTOCOL_PREFIX = PUBLISHED_PROTOCOL_NAME + "://";

How do I save a value into a custom field in JIRA programmatically?

I've spent days trying to find out how to save or update a value into a CustomField programmatically and finally found out how it's done. So I'll make this a question and then answer it as I would have loved to have this question and answer.
There is conflicting documentation on how to save or update a value for a Custom Field in JIRA. I was using:
customField.setCustomFieldValue(CustomField, value);
This does not save the value into the database but it does update the value as far as I can tell. It's only useful if you are using the CustomField further down in a Workflow Post Function transition for example.
I'm using Jira 4.3.2.
How do I persist the the CustomFields value into the JIRA database?
Ok, this is how I'm successfully updating and saving the CustomField value into the JIRA db.
Comments welcome...
private void saveValue(MutableIssue issue, String valueToSave, CustomField
customField) throws FieldLayoutStorageException {
issue.setCustomFieldValue(customField, valueToSave);
Map<String, ModifiedValue> modifiedFields = issue.getModifiedFields();
FieldLayoutItem fieldLayoutItem =
ComponentManager.getInstance().getFieldLayoutManager().getFieldLayout(issue).getFieldLayoutItem(
customField);
DefaultIssueChangeHolder issueChangeHolder = new DefaultIssueChangeHolder();
final ModifiedValue modifiedValue = (ModifiedValue) modifiedFields.get(customField.getId());
customField.updateValue(fieldLayoutItem, issue, modifiedValue, issueChangeHolder);
}
Here is how I do it (for a custom field I programmatically store a random UUID in):
CustomField cfHash = customFieldManager.getCustomFieldObjectByName(...);
IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
try {
Object newHashValue = java.util.UUID.randomUUID().toString();
Object oldHashValue = issue.getCustomFieldValue(cfHash);
issue.setCustomFieldValue(cfHash, newHashValue);
cfHash.updateValue(null, issue, new ModifiedValue(oldHashValue, newHashValue), changeHolder);
...
More or less the same as you but with another way to get the ModifiedValue-Object.
Here a solution that works for me in JIRA 6.4.7 to update a custom field value. Actually Im updating a single select field, therefore I have to get the Option for it:
MutableIssue issue = issueManager.getIssueByCurrentKey(issueKey);
FieldConfig relevantConfig = customField.getRelevantConfig(issue);
// if you use a text field use String. or double for numeric
Option optionForValue = optionsManager.getOptions(relevantConfig).getOptionForValue(option, null);
issue.setCustomFieldValue(customField,optionForValue);
Map<String, ModifiedValue> modifiedFields = issue.getModifiedFields();
FieldLayoutItem fieldLayoutItem =
fieldLayoutManager.getFieldLayout(issue).getFieldLayoutItem(customField);
DefaultIssueChangeHolder issueChangeHolder = new DefaultIssueChangeHolder();
final ModifiedValue modifiedValue = modifiedFields.get(customField.getId());
customField.updateValue(fieldLayoutItem, issue, modifiedValue, issueChangeHolder);
I had the same issue and had it resolved using this plugin, fyi=)

Categories

Resources