Implemented using Spring MVC, rest service.
Display file on screen or download as attachment.
@Controller
public class FileController
{
private static final Logger log = LoggerFactory.getLogger(FileController.class);
@RequestMapping(value = "/file/{id}", method = RequestMethod.GET)
public void getFileViaAttachment(@PathVariable String id, HttpServletResponse response)
{
log.info("getFile " + id);
try
{
String content = "testing file download";
String fileName = "mojFileIzStringa.txt";
InputStream in = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
IOUtils.copy(in, response.getOutputStream());
response.setContentType("application/text");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
response.flushBuffer();
}
catch (IOException e)
{
e.printStackTrace();
}
}
@RequestMapping(value = "/many", method = RequestMethod.GET)
public void getManyLinesViaAttachment(HttpServletResponse response)
{
log.info("getManyLines ");
try
{
String[] linesArrays = { "prva linija", "druga linija", "xxx linija" };
List<String> linesList = Arrays.asList(linesArrays);
String fileName = "mojFileSpunoLinija.csv";
IOUtils.writeLines(linesList, null, response.getOutputStream(), StandardCharsets.UTF_8);
response.setContentType("application/text");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
response.flushBuffer();
}
catch (IOException e)
{
e.printStackTrace();
}
}
// every line in new row
@RequestMapping(value = "/getFileAsItIs", method = RequestMethod.GET, produces = "text/plain")
@ResponseBody
public String getFileAsItIs()
{
log.info("getFileAsItIs ");
String[] linesArrays = { "prva linija", "druga linija", "xxx linija" };
List<String> linesList = Arrays.asList(linesArrays);
StringBuilder sb = new StringBuilder();
for (String s : linesList)
{
sb.append(s).append(System.lineSeparator());
}
return sb.toString();
}
}
Monday, March 23, 2015
Change constraint
Table simmapping has foreign key to msisdn on 2 columns msisdn and providerId.
Change key so you can do update on providerId
Database: postgres.
ALTER TABLE public.simmapping
DROP CONSTRAINT fk_simmapping_msisdn,
ADD CONSTRAINT fk_simmapping_msisdn
FOREIGN KEY (msisdn, msisdn_providerid)
REFERENCES msisdn(msisdn, providerid)
ON UPDATE CASCADE;
Change key so you can do update on providerId
Database: postgres.
ALTER TABLE public.simmapping
DROP CONSTRAINT fk_simmapping_msisdn,
ADD CONSTRAINT fk_simmapping_msisdn
FOREIGN KEY (msisdn, msisdn_providerid)
REFERENCES msisdn(msisdn, providerid)
ON UPDATE CASCADE;
Tuesday, March 3, 2015
JPA Composite Key
Create class that will represent composite key and has implemented hashCode and equals methods.
Class that uses this composite key needs to be annotated with @IdClass and have same fields (msisdn and providedId as in example).
Ref:
https://stackoverflow.com/questions/13032948/how-to-create-and-handle-composite-primary-key-in-jpa
public class MsisdnCompositeKey implements Serializable
{
private static final long serialVersionUID = 1L;
private String msisdn;
private Long providerId;
.....
Class that uses this composite key needs to be annotated with @IdClass and have same fields (msisdn and providedId as in example).
@Entity
@Table(name = "MSISDN")
@IdClass(value = MsisdnCompositeKey.class)
public class Msisdn
{
private static final long serialVersionUID = 1L;
@Id
private String msisdn;
@Id
private Long providerId;
.....
Ref:
https://stackoverflow.com/questions/13032948/how-to-create-and-handle-composite-primary-key-in-jpa
Subscribe to:
Posts (Atom)