Criteria API doesn't execute query - java

I've got a DAO, which filters lists.
public List<User> filterUser(UserView userView) {
List<User> userList;
Long officeId = userView.officeId;
String firstName = userView.firstName;
String secondName = userView.secondName;
String middleName = userView.middleName;
String position = userView.position;
String docCode = userView.docCode;
String citizenshipCode = userView.citizenshipCode;
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<User> userQuery = criteriaBuilder.createQuery(User.class);
Root<User> userRoot = userQuery.from(User.class);
Join<User, Document> docJoin = userRoot.join("document");
Join<Document, DocType> docTypeJoin = docJoin.join("docType");
Join<Document, Country> countryJoin = docJoin.join("country");
userQuery.select(userRoot);
userQuery.where(criteriaBuilder.equal(userRoot.get("office"), officeId));
if (firstName != null) {
userQuery.where(criteriaBuilder.equal(userRoot.get("firstName"), firstName));
}
if (secondName != null) {
userQuery.where(criteriaBuilder.equal(userRoot.get("secondName"), secondName));
}
if (middleName != null) {
userQuery.where(criteriaBuilder.equal(userRoot.get("middleName"), middleName));
}
if (position != null) {
userQuery.where(criteriaBuilder.equal(userRoot.get("position"), position));
}
if (docCode != null) {
userQuery.where(criteriaBuilder.equal(docTypeJoin.get("code"), docCode));
}
if (citizenshipCode != null) {
userQuery.where(criteriaBuilder.equal(countryJoin.get("code"), citizenshipCode));
}
userList = em.createQuery(userQuery).getResultList();
return userList;
}
If I request fields officeId,firstName, secondName and etc everything works correctly.
Problem with docCode and citizenshipCode. For example when I send officeId with docCode a filter works only for docCode and nothing for officeId.
In case I change the rows in places like this:
userQuery.where(criteriaBuilder.equal(docTypeJoin.get("code"), docCode));
and after
userQuery.where(criteriaBuilder.equal(userRoot.get("office"), officeId));
I've got list with only officeId and nothing fordocCode.

Related

QuerySyntaxException hibernate invalid path on EntityManager createQuery

I am sending from the frontend a value to search on two properties of my entity Producto. That properties are Codigo and Descripcion.
The issue is that when the line TypedQuery<Long> typedQuery = em.createQuery(queryCount); hits, this exception is thrown:
queryString= org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path:
'generatedAlias1._codigo' [select count(generatedAlias0) from
com.its.entidades.db.Producto as generatedAlias0 where (
generatedAlias1._codigo like :param0 ) and (
generatedAlias1._descripcion like :param1 )]
detailMessage= Invalid path: 'generatedAlias1._codigo'
The weird thing is that if I comment the quoted line, and in consequence the two lines below, everything runs as expected.
But I need to get the total of the registers filtered, so I need to count them.
ProductoService.java
#Override
public ServiceResponse<List<Producto>> ObtenerListaPaginada(ParametrosListadoModelo parametros) {
ServiceResponse<List<Producto>> ret = new ServiceResponse<>();
ret.setListadoModelo(parametros);
try {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Producto> query = cb.createQuery(Producto.class);
CriteriaQuery<Long> queryCount = cb.createQuery(Long.class);
queryCount.select(cb.count(queryCount.from(Producto.class)));
Root<Producto> entity = query.from(Producto.class);
TypedQuery<Producto> tq = null;
if (parametros.getBusqueda() != null && !parametros.getBusqueda().isEmpty()) {
String queryFilter = "%" + parametros.getBusqueda() + "%";
List<Predicate> predicates = new ArrayList<>();
predicates.add(cb.like(entity.<String>get("_codigo"), queryFilter));
predicates.add(cb.like(entity.<String>get("_descripcion"), queryFilter));
query.where(predicates.toArray(new Predicate[]{}));
queryCount.where(predicates.toArray(new Predicate[]{}));
}
// Count for total
TypedQuery<Long> typedQuery = em.createQuery(queryCount);
Long count = typedQuery.getSingleResult();
ret.getListadoModelo().setTotalRegistros(count);
// Order by
if (parametros.getCampoOrdenamiento().equals("codigo"))
parametros.setCampoOrdenamiento("_codigo");
if (parametros.getCampoOrdenamiento().equals("descripcion"))
parametros.setCampoOrdenamiento("_descripcion");
query.orderBy(parametros.getDireccionOrdenamiento().equals("ASC") ? cb.asc(entity.get(parametros.getCampoOrdenamiento())) : cb.desc(entity.get(parametros.getCampoOrdenamiento())));
// Paginator
tq = em.createQuery(query);
tq.setFirstResult((int) ((parametros.getNumeroPagina() - 1) * parametros.getCantidadElementos()));
tq.setMaxResults((int) (parametros.getCantidadElementos()));
ret.setData(tq.getResultList());
} catch (Exception ex) {
ret.getErrores().add(new ServicioError(ex));
}
return ret;
}
Producto.java
#Entity(name = "Producto")
public class Producto {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name = "ProductoID")
private int _productoID;
#Column(name = "cCodigo")
private String _codigo;
#Column(name="cDescripcion")
private String _descripcion;
#JsonProperty("codigo")
public String getCodigo() {
return _codigo;
}
#JsonProperty("codigo")
public void setCodigo(String _codigo) {
this._codigo = _codigo;
}
#JsonProperty("descripcion")
public String getDescripcion() {
return _descripcion;
}
#JsonProperty("descripcion")
public void setDescripcion(String _descripcion) {
this._descripcion = _descripcion;
}
}
Why is this happening?
Finally solved refactoring:
try{
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Producto> query = cb.createQuery(Producto.class);
CriteriaQuery<Long> queryCount = cb.createQuery(Long.class);
Root<Producto> entityRoot = queryCount.from(query.getResultType());
queryCount.select(cb.count(entityRoot));
Root<Producto> entity = query.from(Producto.class);
TypedQuery<Producto> tq;
//And the rest of the code is the same as the original one.
//...
}

Neo4j browser freezes the whole system

I'm using an embedded Neo4j instance in my Spring Boot project (I'm using Spring JPA and Neo4j separately and I'm not using Spring-Boot-Neo4j stuff) and I want to visualise the graph I build using the Neo4j browser that I downloaded from here with the addition of a custom init.coffee that allows it to display images inside the nodes
This is the code I use in order to build my graph. The "buildGraph" function is executed when a request is being received by one of my RestControllers
(the reason I include all of my business logic implementation is because it may help detecting relationships/nodes etc being created/handled in a wrong way)
#Component
public class GraphBuilder
{
private String dbPath;
private int numberOFConnectionsForKeyIndividuals;
private String neo4jBoltAddress;
#Autowired
PersonService personService;
private final GraphDatabaseService graphDb;
public GraphBuilder(#Value("${dbPath}") String dbPath,
#Value("${neo4jBoltAddress}") String neo4jBoltAddress,
#Value("${numberOFConnectionsForKeyIndividuals}") int numberOFConnectionsForKeyIndividuals)
{
GraphDatabaseSettings.BoltConnector bolt = GraphDatabaseSettings.boltConnector( "0" );
this.dbPath = dbPath;
this.neo4jBoltAddress = neo4jBoltAddress;
this.numberOFConnectionsForKeyIndividuals = numberOFConnectionsForKeyIndividuals;
graphDb = new GraphDatabaseFactory()
.newEmbeddedDatabaseBuilder( new File(dbPath) )
.setConfig( bolt.type, "BOLT" )
.setConfig( bolt.enabled, "true" )
.setConfig( bolt.address, neo4jBoltAddress )
.newGraphDatabase();
registerShutdownHook( graphDb );
}
public void buildGraph()
{
List<Long> AsIDs = new ArrayList<>();
Map<Long,Long> personIdToNodeMap = new HashMap<>();
Map<String,List<Long>> nameToId = new HashMap<>();
Map<Long,List<Association>> associationsMap = new HashMap<>();
try ( Transaction tx = graphDb.beginTx() )
{
Schema schema = graphDb.schema();
for(Person person : personService.findAllPersons())
{
Node personNode = graphDb.createNode(new Label() {
#Override
public String name() {
return "Person";
}
});
//mapping persons to their respective nodes
personIdToNodeMap.put(person.getPersonId(),personNode.getId());
//mapping names to the ids of the persons
if(nameToId.get(person.getName()) == null)
{
nameToId.put(person.getName(), new ArrayList<>());
}
nameToId.get(person.getName()).add(person.getPersonId());
personNode.setProperty("Name", person.getName());
for(int a = 0 ; a < person.getAliases().size() ; a++)
{
personNode.setProperty("Alias " + a+1, person.getAliases().get(a).getAlias());
}
personNode.setProperty("Additional Name Information", person.getAdditionalNameInformation() != null ? person.getAdditionalNameInformation() : "");
personNode.setProperty("Id", person.getPersonId());
personNode.setProperty("Date of Birth", person.getDob() != null ? person.getDob() : "");
for(int f = 0 ; f < person.getFacebook().size() ; f++)
{
personNode.setProperty("Facebook " + f+1, person.getFacebook().get(f).getFacebookPage() + " (" + person.getFacebook().get(f).getAdditionalFacebookPageInformation() + ")");
}
personNode.setProperty("Additional Information", person.getInfo() != null ? person.getInfo() : "");
personNode.setProperty("image_url","http://localhost:8888/files/"+person.getPictureFilePath());
personNode.setProperty("Node Type", "Person");
if(person.getAssociations().size() > numberOFConnectionsForKeyIndividuals)
{
personNode.setProperty("Key_Individual","Yes");
}
for(A A : person.getAs())
{
Node ANode = graphDb.createNode(new Label() {
#Override
public String name() {
return "A";
}
});
ANode.setProperty("A", A.getA());
//TODO elaborate more on the A with additional properties
ANode.setProperty("Node Type", "A");
personNode.createRelationshipTo( ANode, EdgeTypes.HAS_DONE );
ANode.setProperty("image_url","http://localhost:8888/images/A.png");
AsIDs.add(ANode.getId());
}
for(Association association : person.getAssociations())
{
if(associationsMap.get(person.getPersonId()) == null)
{
associationsMap.put(person.getPersonId(), new ArrayList<>());
}
associationsMap.get(person.getPersonId()).add(association);
}
}
//Validating and building the association edges
//iterating through the nodes
for(Long personFromId : associationsMap.keySet())
{
//iterating through the associations registered for the node
for(Association associationFrom : associationsMap.get(personFromId))
{
String personNameFrom = associationFrom.getPersonNameFrom();
String personNameTo = associationFrom.getPersonNameTo();
//iterating through the persons whose name matches the other end of the association
if(nameToId.get(personNameTo) != null)
{
for(Long personToId : nameToId.get(personNameTo))
{
//iterating through the associations of the person at the other end of the association
if(associationsMap.get(personToId) != null)
{
List<Association> associationsToRemove = new ArrayList<>();
for(Association associationTo : associationsMap.get(personToId))
{
if(associationTo.getPersonNameTo().equals(personNameFrom) && nameToId.get(personNameFrom).contains(personFromId))
{
if(nameToId.get(personNameFrom).size() == 1)
{
Relationship relationship = graphDb.getNodeById(personIdToNodeMap.get(personFromId))
.createRelationshipTo( graphDb.getNodeById(personIdToNodeMap.get(personToId)), EdgeTypes.ASSOCIATES_WITH );
if(associationFrom.getType() != null)
{
relationship.setProperty("Association Type",associationFrom.getType());
}
associationsToRemove.add(associationTo);
}
else
{
boolean alreadyConnected = false;
for(Relationship rel : graphDb.getNodeById(personIdToNodeMap.get(personFromId)).getRelationships())
{
if( ( rel.getOtherNode(graphDb.getNodeById(personIdToNodeMap.get(personFromId))).
equals(graphDb.getNodeById(personIdToNodeMap.get(personToId))) ) )
{
alreadyConnected = true;
break;
}
}
if(!alreadyConnected)
{
Relationship relationship = graphDb.getNodeById(personIdToNodeMap.get(personFromId))
.createRelationshipTo( graphDb.getNodeById(personIdToNodeMap.get(personToId)), EdgeTypes.PROBABLY_ASSOCIATES_WITH );
if(associationFrom.getType() != null)
{
relationship.setProperty("Association Type",associationFrom.getType());
}
}
// associationsToRemove.add(associationTo);
}
}
}
associationsMap.get(personToId).removeAll(associationsToRemove);
}
}
}
}
}
tx.success();
}
Map<Long,List<String>> AToNamesMap = new HashMap<>();
//detecting names referred in the A's description
try(Transaction txAs = graphDb.beginTx() )
{
for(Long id : AsIDs)
{
Node ANode = graphDb.getNodeById(id);
String A = (String) ANode.getProperty("A");
for(String name : nameToId.keySet())
{
if(A.contains(name)) {
if(AToNamesMap.get(id) == null)
{
AToNamesMap.put(id,new ArrayList<>());
}
AToNamesMap.get(id).add(name);
}
}
}
List<Long> groupedAs = new ArrayList<>();
for(Long id : AsIDs)
{
if(AToNamesMap.get(id)!= null && AToNamesMap.get(id).size() > 1)
{
for(Long otherAID : AToNamesMap.keySet())
{
if(id != otherAID && !groupedAs.contains(otherAID) && !groupedAs.contains(id))
{
if(compareNamesLists(AToNamesMap.get(id), AToNamesMap.get(otherAID)))
{
Relationship rel = graphDb.getNodeById(otherAID).getSingleRelationship(EdgeTypes.HAS_DONE,Direction.INCOMING);
Node otherPersonNode = rel.getStartNode();
if(nameToId.get(otherPersonNode.getProperty("Name")) != null && nameToId.get(otherPersonNode.getProperty("Name")).size() > 1)
{
otherPersonNode.createRelationshipTo(graphDb.getNodeById(id), EdgeTypes.HAS_PROBABLY_DONE);
}
else
{
otherPersonNode.createRelationshipTo(graphDb.getNodeById(id), EdgeTypes.HAD_DONE);
}
rel.delete();
graphDb.getNodeById(otherAID).delete();
groupedAs.add(otherAID);
}
}
}
}
groupedAs.add(id);
}
txAs.success();
}
}
private static void registerShutdownHook( final GraphDatabaseService graphDb )
{
// Registers a shutdown hook for the Neo4j instance so that it
// shuts down nicely when the VM exits (even if you "Ctrl-C" the
// running application).
Runtime.getRuntime().addShutdownHook( new Thread()
{
#Override
public void run()
{
graphDb.shutdown();
}
} );
}
}
When I open the browser and connect it to the bolt that I expose for my embedded neo4j, the browser is able to show all the nodes in my graph database (at the moment less than 100) and it then freezes, causing the entire system to freeze (MacBook Pro 2016, 16GB). This happens around 3/5 times.
I know that the way I make my transactions is not ideal, but as I said all this processing happens before the neo4j browser starts.
Can you advise me on how to solve this issue?
Can you see anything in my code (connection left open etc) Is this a known issue for the neo4j browser?

Can't Update Multi record Error Transaction is Inactive

I am trying to update multi records with Transaction ebean, however when I am trying more than one record, there is an error on the screen :
[IllegalStateException: Transaction is Inactive]
any idea please ?
public Result savemulti(String selected) throws PersistenceException {
Form<Computer> computerForm = formFactory.form(Computer.class).bindFromRequest();
if(computerForm.hasErrors()) {return badRequest(views.html.computers.editMulti.render(AuthorisedUser.findByEmail(request().username()), computerForm, selected));}
java.util.Date dtcreate = new java.util.Date();
String connectedEmail = ctx().session().get("email");
AuthorisedUser singleUser = AuthorisedUser.findByEmail(connectedEmail);
String[] ids = selected.split(";");
Transaction txn = Ebean.beginTransaction();
try{
for (String temp : ids){
Computer savedComputer = Computer.find.byId(Long.parseLong(temp));
if (savedComputer != null){
Computer newComputerData = computerForm.get();
savedComputer.company = newComputerData.company;
savedComputer.discontinued = newComputerData.discontinued;
savedComputer.introduced = newComputerData.introduced;
savedComputer.name = newComputerData.name;
savedComputer.status = newComputerData.status;
savedComputer.moddt = new java.sql.Timestamp(dtcreate.getTime());
savedComputer.modby = singleUser.userName;
savedComputer.site = singleUser.site;
savedComputer.update();
flash("success", "Computer [ " + computerForm.get().name + " ] has been updated");
txn.commit();
}
}
} finally {
txn.end();
}
return GO_HOME;
}
I just put for before transaction and it works perfectly, thank you.
public Result savemulti(String selected) throws PersistenceException {
Form<Computer> computerForm = formFactory.form(Computer.class).bindFromRequest();
if(computerForm.hasErrors()) {return badRequest(views.html.computers.editMulti.render(AuthorisedUser.findByEmail(request().username()), computerForm, selected));}
java.util.Date dtcreate = new java.util.Date();
String connectedEmail = ctx().session().get("email");
AuthorisedUser singleUser = AuthorisedUser.findByEmail(connectedEmail);
String[] ids = selected.split(";");
for (String temp : ids)
{
Transaction txn = Ebean.beginTransaction();
try
{
Computer savedComputer = Computer.find.byId(Long.parseLong(temp));
if (savedComputer != null)
{
Computer newComputerData = computerForm.get();
savedComputer.company = newComputerData.company;
savedComputer.active = newComputerData.active;
savedComputer.discontinued = newComputerData.discontinued;
savedComputer.introduced = newComputerData.introduced;
savedComputer.name = newComputerData.name;
savedComputer.status = newComputerData.status;
savedComputer.moddt = new java.sql.Timestamp(dtcreate.getTime());
savedComputer.modby = singleUser.userName;
savedComputer.site = singleUser.site;
savedComputer.update();
txn.commit();
}
}
finally { txn.end(); }
}
flash("success", "Computers [ " + selected + " ] has been updated");
return GO_HOME;
}

java.lang.ClassCastException: com.mongodb.FindIterableImpl cannot be cast to org.bson.Document

I have following code
public class UserDAO {
private final MongoCollection<Document> usersCollection;
private Random random = new SecureRandom();
public UserDAO(final MongoDatabase blogDatabase) {
usersCollection = blogDatabase.getCollection("users");
}
public boolean addUser(String username, String password, String email) {
String passwordHash = makePasswordHash(password, Integer.toString(random.nextInt()));
Document doc = new Document("_id", username).append("password", passwordHash);
if (email != null && !email.equals("")) {
doc = new Document("_id", username)
.append("password", passwordHash)
.append("email", email);
}
try {
usersCollection.insertOne(doc);
return true;
} catch (MongoWriteException e) {
if (e.getError().getCategory().equals(ErrorCategory.DUPLICATE_KEY)) {
System.out.println("Username already in use: " + username);
return false;
}
throw e;
}
}
public Document validateLogin(String username, String password) {
Document user = null;
user = (Document) usersCollection.find(new BasicDBObject("_id", username));// at this line I'm getting error
if (user == null) {
System.out.println("User not in database");
return null;
}
String hashedAndSalted = user.get("password").toString();
String salt = hashedAndSalted.split(",")[1];
if (!hashedAndSalted.equals(makePasswordHash(password, salt))) {
System.out.println("Submitted password is not a match");
return null;
}
return user;
}
}
I want to get one document/record but I'm getting error
java.lang.ClassCastException: com.mongodb.FindIterableImpl cannot be cast to org
.bson.Document
at course.UserDAO.validateLogin(UserDAO.java:94)
at course.BlogController$6.doHandle(BlogController.java:231)
at course.BlogController$FreemarkerBasedRoute.handle(BlogController.java
:92)
at spark.webserver.MatcherFilter.doFilter(MatcherFilter.java:139)
at spark.webserver.JettyHandler.doHandle(JettyHandler.java:54)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandle
r.java:179)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.j
ava:136)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper
.java:97)
at org.eclipse.jetty.server.Server.handle(Server.java:451)
at org.eclipse.jetty.server.HttpChannel.run(HttpChannel.java:252)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.jav
a:266)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConn
ection.java:240)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPoo
l.java:596)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool
.java:527)
at java.lang.Thread.run(Thread.java:745)
are there any mistakes in my code, any solutions.
You can do this also:
Document user = null;
user = usersCollection.find(eq("_id", username)).first();
use Filter.eq() to compare the equality of the username, If you have used import static com.mongodb.client.model.Filters.eq; then only use eq() method.
And use first() it returns the first Document from the collection.
OK I've found this answer that is make some changes in validateLogin() as
public Document validateLogin(String username, String password) {
FindIterable<Document> user = null; // Change to get the object of FindIterable<Document>
user = usersCollection.find(new Document("_id", username) );// give Document as the find() argument
if (user == null) {
System.out.println("User not in database");
return null;
}
String hashedAndSalted = user.first().get("password").toString();// get user.first()
String salt = hashedAndSalted.split(",")[1];
if (!hashedAndSalted.equals(makePasswordHash(password, salt))) {
System.out.println("Submitted password is not a match");
return null;
}
return user.first();//get user.first()
}
hence the issue is resolved.
by just adding first() to your search query will save you all the trouble.
Bson filter = new Document("_id",username);
Document user = coll.find().filter(filter).first();

Java export data to (custom-format) text file [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I was asked to work on this back-end scheduled job that export some customers data (from an e-commerce DB) to a custom-format text file. The code that follows is what I found.
I just would like to delete it all, but I can't. Would it be possible for me to improve this without changing it so much?
public class AConverter implements CustomerConverter {
protected final Logger LOG = LoggerFactory.getLogger(AConverter.class);
private final static String SEPARATOR = ";";
private final static String CR = "\n";
public String create(Customer customer) {
if (customer == null)
return null;
LOG.info("Exporting customer, uidpk: {}, userid: {}", customer.getUidPk(), customer.getUserId());
StringBuilder buf = new StringBuilder();
buf.append("<HEAD>");
buf.append(SEPARATOR);
buf.append(String.valueOf(customer.getUidPk()));
buf.append(SEPARATOR);
byte[] fullName = null;
try {
fullName = customer.getFullName().getBytes("UTF-8");
} catch (UnsupportedEncodingException e1) {
fullName = customer.getFullName().getBytes();
}
String name = null;
try {
name = new String(fullName, "UTF-8");
} catch (UnsupportedEncodingException e) {
name = customer.getFullName();
}
buf.append(limitString(name, 40));
buf.append(SEPARATOR);
final CustomerAddress preferredShippingAddress = customer.getPreferredShippingAddress();
if (preferredShippingAddress != null) {
final String street1 = preferredShippingAddress.getStreet1();
if (street1 != null) {
buf.append(limitString(street1, 40));
}
} else {
buf.append(" ");
}
buf.append(SEPARATOR);
final String addressStr = buildAddressString(customer);
buf.append(limitString(addressStr, 40));
buf.append(SEPARATOR);
buf.append(limitString(customer.getEmail(), 80));
buf.append(SEPARATOR);
if (preferredShippingAddress!=null && preferredShippingAddress.getStreet2() != null) {
buf.append(limitString(preferredShippingAddress.getStreet2(), 40));
} else {
buf.append(" ");
}
buf.append(SEPARATOR);
buf.append(limitString(customer.getPhoneNumber(), 25));
buf.append(SEPARATOR);
if (preferredShippingAddress!=null) {
if(preferredShippingAddress.getCountry()!=null) {
buf.append(preferredShippingAddress.getCountry());
} else {
buf.append(" ");
}
} else {
buf.append(" ");
}
buf.append(SEPARATOR);
if (preferredShippingAddress!=null) {
if(preferredShippingAddress.getCountry()!=null) {
buf.append(preferredShippingAddress.getCountry());
} else {
buf.append(" ");
}
} else {
buf.append(" ");
}
buf.append(SEPARATOR);
String fodselsnummer = " ";
try {
Map<String, AttributeValue> profileValueMap = customer.getProfileValueMap();
AttributeValue attributeValue = profileValueMap.get("CODE");
fodselsnummer = attributeValue.getStringValue();
} catch (Exception e) {
}
buf.append(fodselsnummer);
buf.append(CR);
final String string = buf.toString();
return string;
}
private String buildAddressString(Customer customer) {
final CustomerAddress preferredShippingAddress = customer.getPreferredShippingAddress();
if (preferredShippingAddress != null) {
final String zipOrPostalCode = preferredShippingAddress.getZipOrPostalCode();
final String city = preferredShippingAddress.getCity();
if (zipOrPostalCode != null && city != null) {
return zipOrPostalCode + " " + city;
} else if(zipOrPostalCode == null && city != null) {
return city;
} else if(zipOrPostalCode != null && city == null) {
return zipOrPostalCode;
}
}
return " ";
}
private String limitString(String value, int numOfChars) {
if (value != null && value.length() > numOfChars)
return value.substring(0, numOfChars);
else
return value;
}
}
You say you want to improve it, you'd like to delete it, but you can't. I'm not sure why you can't. I also don't understand why you'd want to delete it. But it sounds to me like the kind of attitude I used to have before I read Refactoring by Martin Fowler. I would strongly suggest you read that book, if you haven't already.
It is certainly possible to improve this code (or any code) without rewriting it all. The most obvious improvements would be to eliminate some of the repetitive code in the create method by creating some utility methods, and then breaking up the create method into several smaller methods à la template methods.
Also, there is a questionable bit of code in the create method that turns the customer's name into a UTF-8 byte stream and then back into a string. I can't imagine what that's for. Finally, it returns null if the customer is null. That is unlikely to be necessary or wise.
For fun, I decided to do a little refactoring on this code. (Note that proper refactoring involves unit tests; I don't have any tests for this code and have not even compiled the code below, much less tested it.) Here is one possible way you could rewrite this code:
public class AConverter implements CustomerConverter {
protected final Logger LOG = LoggerFactory.getLogger(AConverter.class);
private final static String SEPARATOR = ";";
private final static String CR = "\n";
public String create(Customer customer) {
if (customer == null) throw new IllegalArgumentException("no cust");
LOG.info("Exporting customer, uidpk: {}, userid: {}",
customer.getUidPk(), customer.getUserId());
StringBuilder buf = new StringBuilder();
doHead(buf, customer);
doAddress(buf, customer);
doTail(buf, customer);
return buf.toString();
}
private void doHead(StringBuilder buf, Customer customer) {
append(buf, "<HEAD>");
append(buf, String.valueOf(customer.getUidPk()));
append(buf, limitTo(40, customer.getFullName()));
}
private void doAddress(StringBuilder buf, Customer customer) {
append(buf, limitTo(40, street1of(customer)));
append(buf, limitTo(40, addressOf(customer)));
append(buf, limitTo(80, customer.getEmail()));
append(buf, limitTo(40, street2of(customer)));
append(buf, limitTo(25, customer.getPhoneNumber()));
append(buf, countryOf(customer));
append(buf, countryOf(customer));
}
private void doTail(StringBuilder buf, Customer customer) {
buf.append(fodselsnummerOf(customer));
buf.append(CR);
}
private void append(StringBuilder buf, String s) {
buf.append(s).append(SEPARATOR);
}
private String street1of(Customer customer) {
final CustomerAddress shipto = customer.getPreferredShippingAddress();
if (shipto == null) return " ";
if (shipto.getStreet1() != null) return shipto.getStreet1();
return " ";
}
private String street2of(Customer customer) {
final CustomerAddress shipto = customer.getPreferredShippingAddress();
if (shipto == null) return " ";
if (shipto.getStreet2() != null) return shipto.getStreet2();
return " ";
}
private String addressOf(Customer customer) {
final CustomerAddress shipto = customer.getPreferredShippingAddress();
if (shipto == null) return " ";
final String post = preferredShippingAddress.getZipOrPostalCode();
final String city = preferredShippingAddress.getCity();
if (post != null && city != null) return post + " " + city;
if (post == null && city != null) return city;
if (post != null && city == null) return post;
return " ";
}
private String countryOf(Customer customer) {
final CustomerAddress shipto = customer.getPreferredShippingAddress();
if (shipto == null) return " ";
if (shipto.getCountry() != null) return shipto.getCountry();
return " ";
}
private String limitTo(int numOfChars, String value) {
if (value != null && value.length() > numOfChars)
return value.substring(0, numOfChars);
return value;
}
private String fodelsnummerOf(Customer customer) {
try {
Map<String, AttributeValue> profileValueMap =
customer.getProfileValueMap();
AttributeValue attributeValue = profileValueMap.get("CODE");
return attributeValue.getStringValue();
} catch (Exception e) {
return " ";
}
}
}
I also notice that there is a problem with your format for the custom-format text file if any of the fields of the customer data (email address, for example) happen to have a semicolon in them, because that is your separator character. I trust that is a known issue?

Categories

Resources